HttpWebRequest使用基本身份validation
我试图通过一个身份validation请求,模拟了“基本身份validation请求”,我们习惯看到为此行为设置IIS时看到。
url是: https : //telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC = 22&SC = 1&ST = 2
(警告:https!)
该服务器在UNIX和Java下作为应用程序服务器运行。
这是我用来连接到这个服务器的代码:
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();
(我从这个网站上的另一篇文章中复制了这个)。 但是我从服务器收到这个答案:
底层连接已closures:发送时发生意外错误。
我想我尝试了一切可能的任务,我在C#上的知识必须提供给我,但是没有…
您也可以自己添加授权标题。
只需将名称设为“授权”,并将值“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);
编辑
我应该使用什么编码将HTTP编码从UTF-8切换到ISO 8859-1 ? 和Jeroen的评论。
我终于明白了!
string url = @"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2"; WebRequest request = WebRequest.Create(url); request.Credentials = GetCredential(); request.PreAuthenticate = true;
这是GetCredential()
private CredentialCache GetCredential() { string url = @"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2"; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; CredentialCache credentialCache = new CredentialCache(); credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(ConfigurationManager.AppSettings["ead_username"], ConfigurationManager.AppSettings["ead_password"])); return credentialCache; }
好极了!
如果您可以使用WebClient
类,则使用基本身份validation变得非常简单:
var client = new WebClient {Credentials = new NetworkCredential("user_name", "password")}; var response = client.DownloadString("https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2");
尝试这个:
System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); credentialCache.Add( new System.Uri("http://www.yoururl.com/"), "Basic", new System.Net.NetworkCredential("username", "password") ); ... ... httpWebRequest.Credentials = credentialCache;
该规范可以被读为“ISO-8859-1”或“未定义”。 你的select。 众所周知,许多服务器使用ISO-8859-1(不pipe你喜不喜欢),当你发送别的东西的时候会失败。
有关更多信息和解决问题的build议,请参阅http://greenbytes.de/tech/webdav/draft-reschke-basicauth-enc-latest.html
对于那些使用RestSharp的人来说 ,使用SimpleAuthenticator可能会失败(可能是因为没有使用ISO-8859-1)。 我设法通过明确发送基本身份validation标头来完成它:
string username = "..."; string password = "..."; public IRestResponse GetResponse(string url, Method method = Method.GET) { string encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{username}:{password}")); var client = new RestClient(url); var request = new RestRequest(method ); request.AddHeader("Authorization", $"Basic {encoded}"); IRestResponse response = client.Execute(request); return response; } var response = GetResponse(url); txtResult.Text = response.Content;