简体   繁体   English

C# RestSharp 获取请求失败

[英]C# RestSharp get request failed

I'm trying to send GET request using C# RestSharp as follows.我正在尝试使用 C# RestSharp 发送 GET 请求,如下所示。

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);

this code not working with timeout.此代码不适用于超时。

but python same code work well.但 python 相同的代码运行良好。

import requests

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

payload={}
headers = {}

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

print(response.text)

What is the cause of this error?这个错误的原因是什么? thank you.谢谢你。

I would suggest trying it in a try catch block and seeing if an error is produced in that can shed some light on the reason.我建议在 try catch 块中尝试它,看看是否产生了错误,这可以阐明原因。 And just in case your target refuses to use Tls12 I would also suggest swapping:万一您的目标拒绝使用 Tls12,我还建议交换:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

for:为了:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls |安全协议类型.Tls | SecurityProtocolType.Tls11 |安全协议类型.Tls11 | SecurityProtocolType.Tls12;安全协议类型.Tls12;

One other thing, i have found adding: ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);另一件事,我发现添加: ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

has also helped me track down issues.还帮助我追踪问题。

so:所以:

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);

What does that get you?这对你有什么好处?

Please try to add below line before execute line.请尝试在执行行之前添加以下行。 It needs TLS12 as i figured out;我发现它需要 TLS12;

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM