Http 无法向服务器发送消息

Http cannot send message to server

我有一个用 .net framework 4.8 编写的网站解决方案,我正在尝试通过 HTTP Post 向支付服务器发送 JSON 消息并获得响应,但没有错误,并且没有响应消息,它甚至不会超时。我在另一个项目中测试了相同的代码,它工作正常,但它是用 .net 5.0(最新的和当前的)编写的。

我不知道为什么相同的代码会产生如此不同的结果(尽管指向不同版本的 dll)。任何人都可以帮助我吗?有这方面的文件吗?我认为在服务器端也可以做一些事情,但我不知道从哪里开始。基本上我需要我的网站能够与支付服务器“对话”,但我被困在这里。

这是我的代码片段

private static readonly HttpClient client = new HttpClient();

public static async Task<string> SubmitPaymentTransaction (string reqJsonstr)
    {
        try
        {
            HttpContent httpContent = new StringContent(reqJsonstr, Encoding.UTF8);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            var message = new HttpRequestMessage();
            message.Content = httpContent;
            message.Method = HttpMethod.Post;
            message.RequestUri = new Uri($"https://payment.com/PayURL");

            HttpResponseMessage response = await client.SendAsync(message);
            response.EnsureSuccessStatusCode();
            string result = await response.Content.ReadAsStringAsync();

            return result;
        }
        catch(Exception error)
        {
            return error.ToString();
        }
        
    }

更新:

我能够捕获错误消息...通过删除等待:client.SendAsync(message)

InnerException = {"Authentication failed because the remote party has closed the transport stream."} 
Message = "The underlying connection was closed: An unexpected error occurred on a send." 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
  at Mall.PayPlugin.Pay.PaymentCore.<SubmitPaymentTransaction>d__19.MoveNext()

我对错误进行了快速搜索,结果发现安全协议是根本问题。我的 Web 应用程序实际上是 运行 在 .net 4.5 上,所以默认情况下它使用 TLS 1.0,这对于 API 服务器来说是不可接受的。指定安全协议,我能够从服务器获得响应,但是,仍然以禁用等待为代价。

特别感谢@paulsm4 的帮助、指导,最重要的是,在我几乎要放弃的时候,通过解决这个问题来激励我。

post 和评论应该清楚地描述问题和我的解决方案。我不想重复自己,所以我只想分享我的代码,它现在对我有用。

private static readonly HttpClient client = new HttpClient();

private enum MySecurityProtocolType
{
    //
    // Summary:
    //     Specifies the Secure Socket Layer (SSL) 3.0 security protocol.
    Ssl3 = 48,
    //
    // Summary:
    //     Specifies the Transport Layer Security (TLS) 1.0 security protocol.
    Tls = 192,
    //
    // Summary:
    //     Specifies the Transport Layer Security (TLS) 1.1 security protocol.
    Tls11 = 768,
    //
    // Summary:
    //     Specifies the Transport Layer Security (TLS) 1.2 security protocol.
    Tls12 = 3072
}

public static async Task<string> SubmitPaymentTransaction (string reqJsonstr)
{
    try
    {
        HttpContent httpContent = new StringContent(reqJsonstr, Encoding.UTF8);
        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

        ServicePointManager.SecurityProtocol = (SecurityProtocolType)(MySecurityProtocolType.Tls12 | MySecurityProtocolType.Tls11 | 
                    MySecurityProtocolType.Tls | MySecurityProtocolType.Ssl3);

        var message = new HttpRequestMessage();
        message.Content = httpContent;
        message.Method = HttpMethod.Post;
        message.RequestUri = new Uri($"https://payment.com/PayURL");

        HttpResponseMessage response = await client.SendAsync(message).ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
        string result = await response.Content.ReadAsStringAsync();

        return result;
    }
    catch(Exception error)
    {
        return error.ToString();
    }
    
}