I Found the solution here: http://social.msdn.microsoft.com/Forums/en-US/51ee408b-2b77-49e2-8066-308e1ca48240/problems-with-basic-http-authentication-over-soap-webservice
when using authentication, the http request will not contain authentication during the initial communication with the server. It expects the server to generate a 401 error, at which point it will send the authentication.
So what happens is the server does not need the authentication, only the webservice running on it. The webservice will not return a 401 error instead it returns an error that the API cannot be accessed via an anonymous user. Even using preauthenticate will not fix the issue, or using credentials cache.
The Solution
Override the GetWebRequest method and insert an authentication header into the HTTP header. The namespace and class must match the namespace and class that you are attempting to generate the headers for.
in c#:
public class serverBasicAuthentication : br.com.cra21.cramg.servercra
{
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request = (HttpWebRequest) base.GetWebRequest(uri);
if (PreAuthenticate)
{
NetworkCredential networkcredential = base.Credentials.GetCredential(uri,"Basic");
if (networkcredential != null)
{
Byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkcredential.UserName + ":" + networkcredential.Password);
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
}
else
throw new ApplicationException("No network credentials") ;
}
return request;
}
}