Http Client 现有连接被远程主机强行关闭
Http Client An existing connection was forcibly closed by the remote host
我在这里做错了什么?
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("mobile_numbers", "5555555555"),
new KeyValuePair<string, string>("message", "Whoo hahahahah")
});
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://api.iwin.co.za");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "134134134");
HttpResponseMessage response = client.PostAsync("iwin/api/v1/messages", formContent).Result;
当我使用上面的 运行 代码时,出现此错误:现有连接被远程主机强行关闭
我检查了几次代码,一切看起来都很好,一些文章表明我遇到的错误是服务器问题,但是当我尝试使用 R-client 时它工作正常
请根据以下代码更改代码
1) https 有问题。您需要为其添加适当的证书。
Dictionary<string, string> formContent= new Dictionary<string, string>();
mapObject.Add("mobile_numbers","5555555555");
mapObject.Add("message","Whoo hahahahah")
var jsonString = JsonConvert.SerializeObject(formContent);
WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 certificate = GetMyX509Certificate();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("https://api.iwin.co.za");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer 134134134");
HttpResponseMessage response = client.PostAsync("/iwin/api/v1/messages", getHttpContent(jsonString)).Result;
另一个将 json 转换为 HttpContent
的函数
private static HttpContent getHttpContent(string jsonString)
{
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
return content;
}
或者你可以绕过证书错误
在开发或处理自签名证书时,您可以通过以下方式忽略不受信任的证书错误:ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
如果客户端和服务器无法就要使用的 TLS 协议版本达成一致,可能会导致这种情况。
我认为 .Net 4.5.2 和更早版本默认为 v1,如果 v1.2 包含在框架中,则它会被停用。 (在 .Net 4.6.* 中,v1.2 是默认值。)
要在 4.5.2 中启用 v1.2,您可以使用:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
(我相信这可以为整个应用程序域启用它?)
无论如何,为我解决了这个问题(针对不同的网站)。
我在这里做错了什么?
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("mobile_numbers", "5555555555"),
new KeyValuePair<string, string>("message", "Whoo hahahahah")
});
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://api.iwin.co.za");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "134134134");
HttpResponseMessage response = client.PostAsync("iwin/api/v1/messages", formContent).Result;
当我使用上面的 运行 代码时,出现此错误:现有连接被远程主机强行关闭 我检查了几次代码,一切看起来都很好,一些文章表明我遇到的错误是服务器问题,但是当我尝试使用 R-client 时它工作正常
请根据以下代码更改代码
1) https 有问题。您需要为其添加适当的证书。
Dictionary<string, string> formContent= new Dictionary<string, string>();
mapObject.Add("mobile_numbers","5555555555");
mapObject.Add("message","Whoo hahahahah")
var jsonString = JsonConvert.SerializeObject(formContent);
WebRequestHandler handler = new WebRequestHandler();
X509Certificate2 certificate = GetMyX509Certificate();
handler.ClientCertificates.Add(certificate);
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("https://api.iwin.co.za");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer 134134134");
HttpResponseMessage response = client.PostAsync("/iwin/api/v1/messages", getHttpContent(jsonString)).Result;
另一个将 json 转换为 HttpContent
的函数 private static HttpContent getHttpContent(string jsonString)
{
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
return content;
}
或者你可以绕过证书错误 在开发或处理自签名证书时,您可以通过以下方式忽略不受信任的证书错误:ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
如果客户端和服务器无法就要使用的 TLS 协议版本达成一致,可能会导致这种情况。
我认为 .Net 4.5.2 和更早版本默认为 v1,如果 v1.2 包含在框架中,则它会被停用。 (在 .Net 4.6.* 中,v1.2 是默认值。)
要在 4.5.2 中启用 v1.2,您可以使用:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
(我相信这可以为整个应用程序域启用它?)
无论如何,为我解决了这个问题(针对不同的网站)。