I’ve just found this very handy little chunk of code to do exactly what you need. It adds the authorization header to the code manually without waiting for the server’s challenge.
public void SetBasicAuthHeader(WebRequest request, String userName, String userPassword)
{
string authInfo = userName + ":" + userPassword;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
}
Use it like this
var request = WebRequest.Create("http://myserver.com/service");
SetBasicAuthHeader(request, userName, password);
var response = request.GetResponse();