First off, the request to the url in Chrome works. It prompts me for the username & password, I enter it, and it displays the page (it's an OData request).
Update: The code works now. It has worked in the past on this same url and it was working for others when I hit this problem. But for a period of time it failed on my system. I'm leaving this question up in case anyone has any suggestions for changes I should make to the below code.
But the following code fails with a 401:
WebRequest request = WebRequest.Create(url);
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
var credentials = new NetworkCredential(username, password);
var credentialCache = new CredentialCache();
Uri uri = new Uri(url);
Uri site = new Uri(uri.GetLeftPart(UriPartial.Authority));
credentialCache.Add(site, "Negotiate", credentials);
credentialCache.Add(site, "Digest", credentials);
credentialCache.Add(site, "Basic", credentials);
credentialCache.Add(new Uri(url), "NTLM", credentials);
request.Credentials = credentialCache;
// this throws the exception
return request.GetResponse();
Throws:
System.Net.WebException HResult=0x80131509 Message=The remote server returned an error: (401) Unauthorized. Source=System
StackTrace: at System.Net.HttpWebRequest.GetResponse() at WindwardReportsDrivers.net.windward.AccessProviders.protocols.AllHttpAccessProvider.Request(String url, NetworkCredential credentials, IDictionary2 headers, Dictionary
2 allProps) in C:\git\Jenova\engine\DotNetEngine\WindwardReportsDrivers\net\windward\AccessProviders\protocols\AllHttpAccessProvider.cs:line 108
User contributions licensed under CC BY-SA 3.0