[英]C# RestSharp get request failed
我正在嘗試使用 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);
此代碼不適用於超時。
但 python 相同的代碼運行良好。
import requests
url = "https://www.futureelectronics.cn/p/2052120"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
這個錯誤的原因是什么? 謝謝你。
我建議在 try catch 塊中嘗試它,看看是否產生了錯誤,這可以闡明原因。 萬一您的目標拒絕使用 Tls12,我還建議交換:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
為了:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | 安全協議類型.Tls | 安全協議類型.Tls11 | 安全協議類型.Tls12;
另一件事,我發現添加: ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
還幫助我追蹤問題。
所以:
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);
這對你有什么好處?
請嘗試在執行行之前添加以下行。 我發現它需要 TLS12;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.