I had some problem getting connected to an Apache server. It returned a 401, not recognizing my credentials.
Hans Liss had a solution to this problem by manually adding credentials to the message header:
HttpWebRequest req =
WebRequest.Create("http://www.SomeApacheServer.com") as HttpWebRequest;
req.Method = "POST";
req.Timeout = 500;
NetworkCredential cred = new NetworkCredential("user", "pwd");
req.Credentials = cred;
string authInfo =
((cred.Domain != null) && (cred.Domain.Length > 0) ?
cred.Domain + @"\" : string.Empty) +
cred.UserName + ":" + cred.Password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
req.Headers["Authorization"] = "Basic " + authInfo;
req.PreAuthenticate = true;
req.ServicePoint.Expect100Continue = false;
This tip was useful for me at least. :)