我正在使用他们的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相关问答推荐

在Docker Windows中,ASP.NET核心容器在自定义端口8080上运行,但ASP.NET容器在固定端口80上运行

Razor中的共享本地化资源文件

格式化 DataBinder.Eval 数据

我可以在 .net 核心中使用 Entity Framework 6(非核心)吗?

如何防止 Azure 网站进入Hibernate 状态?

是否可以在没有那些 .svn 文件夹的情况下从 subversion 签出文件?

asp.net dropdownlist - 在 db 值之前添加空行

ASP.NET GridView 第二个标题行跨越主标题行

测试项目中的 App.config

判断会话是否为空

我应该使用用户名还是用户 ID 来引用 ASP.NET 中经过身份验证的用户

带有邮箱地址参数的 WebApi 方法从 HttpClient 返回 404

ASP.NET 中的全局资源与本地资源

如何使用 ASP.NET 和 jQuery 返回 JSON

如何验证 WebClient 请求?

具有自定义报告创建能力的最佳 ASP.NET 报告引擎

使用 ASP.NET 进行重定向后获取

有没有办法在没有异常类的情况下抛出自定义异常

为什么 IFormFile 显示为空,我该如何解决?

ASP.NET MVC3 局部视图命名约定