我正在使用他们的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 "";
}

然后我得到了这个错误: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仍然默认为仅支持SSL3/TLS 1.0,即使TLS 1.1和1.2受支持.如果这是问题的原因,可以使用以下代码手动将TLS 1.1和1.2添加到启用的值中.

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

Asp.net相关问答推荐

.NET 框架 3.5 和 TLS 1.2

为什么 Web 架构应该是松耦合的?

我可以在一个 Web 项目中有多个 web.config 文件吗?

C# ASP.NET Core Serilog 添加类名和方法到日志(log)

ASP.Net - App_Data & App_Code 文件夹?

为 Web 请求实现速率限制算法的最佳方法是什么?

无法加载文件或程序集 'System.Web.Mvc,版本 = 3.0.0.0,Elmah.MVC 问题

使用 Elmah 处理 Web 服务中的异常

从 IIS 7/8 中的静态内容中删除服务器标头

如何设置asp.net身份cookie过期时间

测试 Web 表单应用程序 (ASP.NET) 的最佳方法是什么

显示 ModalPopupExtender 时如何指定要运行的 javascript

您正在使用哪些身份验证和授权方案 - 为什么?

asp.net 网站上的第一页加载缓慢

使用 .NET 连接到 AS400

解耦 ASP.NET MVC 5 标识以允许实现分层应用程序

如何清除 System.Runtime.Caching.MemoryCache

ASP.Net 哪个用户帐户在 IIS 7 上运行 Web 服务?

System.Web.Caching 还是 System.Runtime.Caching 更适合 .NET 4 Web 应用程序

Appdomain 回收究竟是什么