当前位置: 首页 > 面试题库 >

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

步兴为
2023-03-14
问题内容

我正在使用他们的托管页面集成功能与替代付款进行集成。他们的C#SDK目前没有可用的集成,但是您可以看到它非常简单,我制作了一个小类来发送发布请求并获取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 "";
}

然后,我An existing connection was forcibly closed by the remote host在PostAsync行收到此错误:。这是错误的详细信息:

[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。我一直在阅读相同错误的答案,到目前为止,它们都没有解决我的问题。我在此代码中缺少什么?

谢谢你的帮助


问题答案:

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


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


 类似资料: