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>
API端点使用有效的SSL证书正确承载。此外,如果我使用控制台应用程序/使用邮递员点击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的请求是什么?你能帮我解决上面的问题吗?