[英]Unable to call rest endpoint with non-443 port with a httpClient
[英]HttpClient unable to handshake with an HTTPs endpoint
我需要從WebAPI控制器內部的API端點獲取數據。 我正在使用HttpClient
實現目標。 但是,客戶端似乎無法從端點獲取數據。 connect
調用失敗,並顯示以下錯誤:
HTTPS handshake to test.site failed. System.IO.IOException Unable to read
data from the transport connection: An existing connection was forcibly
closed by the remote host. <An existing connection was forcibly closed by
the remote host>
使用有效的SSL證書正確托管API端點。 此外,如果我使用控制台應用程序/使用郵遞員訪問API,則沒有錯誤,並且可以看到響應。 但是,當我嘗試從API控制器內部以完全相同的方式訪問相同的API時,會看到SSL連接錯誤。
這是客戶端的代碼:
HttpClient client = new HttpClient()
{
BaseAddress = new Uri(uri)
};
var requestMessage = new HttpRequestMessage(
new HttpMethod("GET"),
requestUrl);
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;
client.DefaultRequestHeaders.Add(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var responseMessage = await client.SendAsync(requestMessage);
與控制台應用程序相比,為什么WebAPI發出的connect
請求的行為不同? 您能幫我解決以上問題嗎?
.NET框架的較舊版本默認使用較舊的TLS,而TLS可能會被端點拒絕。 拒絕使用低於TLS v1.2的請求已成為標准。
要解決此問題,您可以使用.NET框架的較新版本,也可以顯式設置ServicePointManager
使用的TLS版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.