Home / Forcing basic http authentication for HttpWebRequest (in .NET/C#) edit
Try Documentalist, my app that offers fast, offline access to 190+ programmer API docs.

HttpWebRequest is a handy .NET class for doing HTTP requests. It has built-in support for HTTP basic authentication via credentials. However, it doesn’t work the way I expected: supplying credentials doesn’t send Authorization HTTP header with the request but only in response to server’s challenge. It often breaks in real world, where servers might not issue a challenge and simply not authenticate a request.
Fortunately fixing it by manually adding Authorization HTTP header to the request is simple and this code snippet shows how to do it:
public void SetBasicAuthHeader(WebRequest req, String userName, String
 string authInfo = userName + “:” + userPassword;
 authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
 req.Headers[“Authorization”] = “Basic ” + authInfo;

Feedback about page:

Optional: your email if you want me to get back to you:

Need fast, offline access to 190+ programmer API docs? Try my app Documentalist for Windows