I am trying to read the source from a page that requires basic authentication. However, using a Header and even Credentials in my HttpWebRequest, I still get a Unauthorized Exception [401] returned.
string urlAddress = URL;
string UserName = "MyUser";
string Password = "MyPassword";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
if (UserName != string.Empty)
{
string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(UserName + ":" + Password));
request.Headers.Add("Authorization", "Basic " + encoded);
System.Net.CredentialCache credentialCache = new System.Net.CredentialCache();
credentialCache.Add(
new System.Uri(urlAddress), "Basic", new System.Net.NetworkCredential(UserName, Password)
);
request.Credentials = credentialCache;
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //<== Throws Exception 401
Fiddler Auth Results
No Proxy-Authenticate Header is present.
WWW-Authenticate Header is present: Basic realm="example"