Jos*_*son 56 c# asp.net ssl httpwebrequest
我的客户通过他们的SSL和Internet Explorer了解了我的问题.他们说他们在访问URL时会遇到信任问题.
我通过HTTPS访问JSON.该网站位于一台服务器上,我在本地计算机上使用控制台应用程序.我试图绕过SSL证书,但是,我的代码仍然失败.
我可以改变HttpWebRequest来解决这个问题吗?
我使用此代码得到此错误:
// You must change the URL to point to your Web server.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.AllowAutoRedirect = true;
// allows for validation of SSL conversations
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
WebResponse respon = req.GetResponse();
Stream res = respon.GetResponseStream();
string ret = "";
byte[] buffer = new byte[1048];
int read = 0;
while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
{
//Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
ret += Encoding.ASCII.GetString(buffer, 0, read);
}
return ret;
Run Code Online (Sandbox Code Playgroud)
glm*_*glm 134
我必须启用其他安全协议版本来解决此问题:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
Run Code Online (Sandbox Code Playgroud)
Jos*_*son 20
我使用此代码启用了日志记录:
http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx
日志位于bin/debug文件夹中(我的控制台应用程序处于调试模式).您需要将安全协议类型添加为SSL 3
我在日志中收到了算法不匹配的情况.这是我的新代码:
// You must change the URL to point to your Web server.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
// Skip validation of SSL/TLS certificate
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
WebResponse respon = req.GetResponse();
Stream res = respon.GetResponseStream();
string ret = "";
byte[] buffer = new byte[1048];
int read = 0;
while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
{
Console.Write(Encoding.ASCII.GetString(buffer, 0, read));
ret += Encoding.ASCII.GetString(buffer, 0, read);
}
return ret;
Run Code Online (Sandbox Code Playgroud)
Cra*_*SFT 11
与现有答案类似,但在PowerShell中:
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.SecurityProtocolType]::Tls11 -bor
[System.Net.SecurityProtocolType]::Tls12 -bor `
[System.Net.SecurityProtocolType]::Tls -bor `
[System.Net.SecurityProtocolType]::Ssl3
Run Code Online (Sandbox Code Playgroud)
然后调用Invoke-WebRequest应该可以工作.
从匿名反馈,良好的建议得到这个:更简单的方式来写这个将是:
[System.Net.ServicePointManager]::SecurityProtocol = @("Tls12","Tls11","Tls","Ssl3")
Run Code Online (Sandbox Code Playgroud)
Jaykul发现了这篇精彩且相关的帖子:验证来自.Net和PowerShell的自签名证书
这可能是由一些事情引起的(最有可能是最不可能的):
服务器的SSL证书不受客户端的信任.最简单的检查是将浏览器指向URL并查看是否获得SSL锁定图标.如果您获得了一个破锁,图标,请单击它以查看问题所在:
服务器需要客户端SSL证书 - 在这种情况下,您必须更新代码以使用客户端证书对请求进行签名.
对于.Net v4.0,我注意到,在创建 HttpWebRequest 对象之前将 ServicePointManager.SecurityProtocol 的值设置为 (SecurityProtocolType)3072会有所帮助。
| 归档时间: |
|
| 查看次数: |
123053 次 |
| 最近记录: |