我对RestSharp提出了一个非常简单的Web请求:
var client = new RestClient("https://website.net");
var request = new RestRequest("/process", Method.GET);
request.AddParameter("cmd", "execute");
IRestResponse response = client.Execute(request);
var content = response.Content;
Console.WriteLine("Response: " + content);
Run Code Online (Sandbox Code Playgroud)
这将返回错误消息:
该请求已中止:无法创建SSL / TLS安全通道
三件事:
他们的证书使用的是TLS 1.2和AES 128,因此它与RC4引起的错误无关。
这是在Visual Studio 2015中的本地Win 10计算机上,目标框架为.NET 4.5.2。
为什么会出现此错误?
编辑:
通过更改代码以使用基本System.Net和WebRequest类并添加以下行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)
正如从这里建议的那样,它有效。所以我猜RestSharp由于某种原因使用了错误的协议?
小智 19
即使对于 Restsharp 库,也可以使用以下行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)
前
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
将工作。
在.NET 4.5,TLS 1.2是可用的,但不是默认启用。
因此,是的,如果需要TLS 1.2,您需要在.NET运行时以某种方式指定它。
仅供参考:在.NET 4.7.NET中,.NET将使用操作系统的默认SSL / TLS版本(大多数现代操作系统将使用默认版本TLS 1.2)
其他不错的SO答:
移动这一行: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
在此行之前: WebRequest request = WebRequest.Create(url);
希望有帮助。
| 归档时间: |
|
| 查看次数: |
9549 次 |
| 最近记录: |