C# – Sending Basic authentication over http

basic-authenticationchttpnetwebrequest

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"

Best Answer

As message says:

No Proxy-Authenticate Header is present.

Then, the workaround:

...
string urlAddress = "http://www.google.com";
string userName = "user01";
string password = "puser01";
string proxyServer = "127.0.0.1";
int proxyPort = 8081;

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(urlAddress);

if (userName != string.Empty)
{
    request.Proxy = new WebProxy(proxyServer, proxyPort)
    {
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(userName, password)
    };

    string basicAuthBase64 = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(string.Format("{0}:{1}", userName, password)));
    request.Headers.Add("Proxy-Authorization", string.Format("Basic {0}", basicAuthBase64));
}

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
    var stream = response.GetResponseStream();
    if (stream != null)
    {
        //--print the stream content to Console
        using (var reader = new StreamReader(stream))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }
}
...

I hope it helps.