C#HttpClient远程主机强制关闭现有连接

时间:2017-09-14 15:29:26

标签: c# asp.net json dotnet-httpclient

我正在使用他们的hosted page integration与替代付款进行整合。他们的C#SDK暂时没有这种集成,但正如你所看到的那样非常简单,我发了一个小类来发送post请求并获得JSON响应。

我测试了我在PostMan和cURL上发送的json对象,两者都工作,也是认证头,所以我认为它们不是问题所在。这是我班级的构造函数:

public AlternativePaymentsCli(string apiSecretKey)
{
    this._apiSecretKey = apiSecretKey;

    _httpClient = new HttpClient();
    _httpClient.DefaultRequestHeaders.Accept
        .Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var authInfo = _apiSecretKey;
    authInfo = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:", _apiSecretKey)));

    // The two line below because I saw in an answer on stackoverflow.
    _httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); 
    _httpClient.DefaultRequestHeaders.Add("Keep-Alive", "3600");

    _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Anything.com custom client v1.0");
    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);

}

我发布数据的方法:

public string CreateHostedPageTransaction(HostedPageRequest req) 
{
    var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };

    // I send this same json content on PostMan and it works. The json is not the problem
    var content = new StringContent(JsonConvert.SerializeObject(req, settings), Encoding.UTF8, "application/json");
    var response = _httpClient.PostAsync(this._baseUrl + "/transactions/hosted", content).Result;
    var responseText = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

    if (response.IsSuccessStatusCode)
        return responseText;

    return "";
}

然后我在PostAsync行中收到此错误:An existing connection was forcibly closed by the remote host。这是错误细节:

[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
   System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) +8192811
   System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult) +47

[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.]
   System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) +294
   System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) +149

[WebException: The underlying connection was closed: An unexpected error occurred on a send.]
   System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) +324
   System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) +137

[HttpRequestException: An error occurred while sending the request.]

我正在使用C#4.5,Asp.Net MVC。我一直在阅读相同错误的答案,到目前为止,他们都没有解决我的问题。我在这段代码中缺少什么?

感谢您的帮助

5 个答案:

答案 0 :(得分:74)

我没有在您的代码示例中看到您在哪里设置_baseUrl的值,但我假设这是在某处完成的。我还假设由于这与付款有关,因此网址为HTTPS。如果远程主机已禁用TLS 1.0并且您的连接以TLS 1.0的形式进入,则可能会导致该行为。我知道C#4.6默认启用了TLS 1.0 / 1.1 / 1.2支持,但我认为C#4.6仍然默认只支持SSL3 / TLS 1.0,即使支持TLS 1.1和1.2。如果这是问题的原因,您可以使用以下代码手动将TLS 1.1和1.2添加到启用的值。

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

答案 1 :(得分:6)

如果您使用的是.Net 4.0,则未定义SecurityProtocolType.Tls11和SecurityProtocolType.Tls2,因此您可以使用下面的硬编码值。

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

答案 2 :(得分:3)

this excellent answer中所述,对于类似问题,无需更改代码即可解决该问题:

将Web项目重新定位为 .Net 4.6 + ,然后按以下方式更新web.config:

<system.web>
  <compilation targetFramework="4.6" /> 
  <httpRuntime targetFramework="4.6" /> 
</system.web>

答案 3 :(得分:0)

这对我有用,第一行确保协议$Src = "C:\Folder3\" $Dst = "C:\Save\" Get-ChildItem -Path $Src -Recurse -Name "File2.txt" | Where-Object {$_.PSIsContainer} | ForEach-Object {[System.IO.Path]::GetDirectoryName($_); New-Item -Path $Dst -Name $_} ssl3,第二行忽略任何潜在的证书错误(忽略并继续,如过期的证书。):

TLS1.2

答案 4 :(得分:0)

对我来说,此错误是由于忘记配置代理服务器引起的。 使用以下代码构造HttpClient为我的.NET 4.7.2应用程序解决了该问题:

var httpClientHandler = new HttpClientHandler { Proxy = WebRequest.GetSystemWebProxy() };
var httpClient = new HttpClient(httpClientHandler);

希望这对某人有帮助。