I have a problem when authenticating HttpWebRequest
The remote server returned an error: (401) Unauthorized.
This is my code:
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
NetworkCredential networkCredential = new NetworkCredential(username, password);
WebRequest http = HttpWebRequest.Create(new Uri(url));
http.Timeout = timeout;
http.PreAuthenticate = true;
http.Credentials = networkCredential;
try
{
HttpWebResponse ws = (HttpWebResponse)http.GetResponse();
Stream str = ws.GetResponseStream();
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
and I tried to set the authorization in another way:
string credentials = String.Format("{0}:{1}", username, password);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
http.Headers.Add("Authorization", authorization);
but that didn't help either.