我正在尝试执行一个身份验证请求,它模仿我们在为此行为设置IIS时经常看到的“基本身份验证请求”。
(警告: 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();
(我从这个网站的另一篇文章中复制了这篇文章)。但是我从服务器收到了这样的回答:
基础连接已关闭:发送时发生意外错误。
我想我已经尝试了所有我在C#上的知识提供给我的可能的任务,但什么都没有…
发布于 2012-12-20 00:25:13
您也可以自己添加authorization标头。
只需将名称"Authorization“和值"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);
编辑
根据What encoding should I use for HTTP Basic Authentication?和Jeroen的评论,将编码从UTF-8切换到ISO8859-1。
发布于 2010-12-03 23:08:57
我终于明白了!
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;
}
耶!
发布于 2015-11-05 17:50:12
如果可以使用WebClient
类,那么使用基本身份验证就会变得很简单:
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");
https://stackoverflow.com/questions/4334521
复制相似问题