HttpWebRequest using Basic authentication
authentication, c#, credentials, webrequest
Solution
You can also just add the authorization header yourself.
Just make the name "Authorization" and the value "Basic BASE64({USERNAME:PASSWORD})"
var username = "abc";
var password = "123";
string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
.GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
Edit
Switched the encoding from UTF-8 to ISO 8859-1 per What encoding should I use for HTTP Basic Authentication? and Jeroen's comment.
Problem
I'm trying to go through an authentication request that mimics the "basic auth request" we're used to seeing when setting up IIS for this behavior. The URL is: https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2 (warning: https!) This server is running under UNIX and Java as application server. This is the code I use to connect to this server: ``` CookieContainer myContainer = new CookieContainer(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2"); request.Credentials = new NetworkCredential(xxx,xxx); request.CookieContainer = myContainer; request.PreAuthenticate = true; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); ``` (I copied this from another post on this site). But I receive this answer from the server: The underlying connection was closed: An unexpected error occurred on a send. I think I tried every possible task my knowledge on C# has to offer me, but nothing...