使用基本身份验证C#HttpWebRequest的 [英] C# HttpWebRequest using Basic authentication
问题描述
我试图去通过模仿了基本身份验证要求认证请求,我们已经习惯了看到了这种行为上的IIS设置的时候。
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.
该网址是:<一href=\"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2\">https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2
(警告!HTTPS)
The URL is: https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2
(warning: https!)
此服务器的UNIX和Java下运行的应用程序服务器。
This server is running under UNIX and Java as application server.
这是code我用它来连接到该服务器:
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.
我想我想尽一切可能的任务,我对C#的知识所提供的我,但没有...
I think I tried every possible task my knowledge on C# has to offer me, but nothing...
推荐答案
您也可以只添加授权头自己。
You can also just add the authorization header yourself.
只要名称授权和值基本BASE64({用户名:密码})
Just make the name "Authorization" and the value "Basic BASE64({USERNAME:PASSWORD})"
String username = "abc";
String password = "123";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
修改
交换的编码从UTF-8到ISO 8859-1每<一个href=\"http://stackoverflow.com/questions/7242316/what-encoding-should-i-use-for-http-basic-authentication\">What编码我应该使用HTTP基本身份验证?以及吉荣的评论。
这篇关于使用基本身份验证C#HttpWebRequest的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!