首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# RestSharp get请求失败

C# RestSharp get请求失败
EN

Stack Overflow用户
提问于 2022-03-29 03:28:20
回答 2查看 252关注 0票数 -1

我试图使用C# RestSharp发送GET请求,如下所示。

代码语言:javascript
复制
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new RestClient("https://www.futureelectronics.cn/p/2052120");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

此代码不适用于超时。

但是python相同的代码运行良好。

代码语言:javascript
复制
import requests

url = "https://www.futureelectronics.cn/p/2052120"

payload={}
headers = {}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

造成这一错误的原因是什么?谢谢。

EN

回答 2

Stack Overflow用户

发布于 2022-03-29 08:37:29

我建议在一个try catch块中试一试,看看是否会产生错误,这可以说明原因。如果你的目标拒绝使用Tls12,我也会建议交换:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

适用于:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3、SecurityProtocolType.Tls、SecurityProtocolType.Tls11、SecurityProtocolType.Tls11、SecurityProtocolType.Tls12;

另一件事,我发现添加: ServicePointManager.ServerCertificateValidationCallback =新的System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

也帮我找到了问题。

因此:

代码语言:javascript
复制
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
var client = new
RestClient("https://www.futureelectronics.cn/p/2052120");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
try
{
    IRestResponse response = client.Execute(request);
}
catch (Exception ex)
{
    Console.WriteLine("Caught exception : " + ex.message)
}
Console.WriteLine(response.Content);

这能给你带来什么?

票数 1
EN

Stack Overflow用户

发布于 2022-07-18 14:18:40

请尝试在执行行之前添加以下行。它需要TLS12,就像我想的那样;

代码语言:javascript
复制
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71656107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档