HttpClient 发布到 api,获取 System.IO.IOException:身份验证失败,因为远程方已关闭传输流

HttpClient Posting to api, getting System.IO.IOException: Authentication failed because the remote party has closed the transport stream

我正在使用 VS RestClient 插件成功调用服务(为保护机密所做的更改)

POST https://zzz-apis-xyz.com/abc/abc-data/v1/abcbusinessdata HTTP/1.1
X-Special-Client-Id: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
X-Special-Client-Secret: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
accept: application/json 
clientID: cccccc 
content-type: application/json 
abcDistributedKey: ---guid--- 
systemName: chicken

{
    "business": "Hilton",
    "address": "7930 Jones Branch Drive",
    "city": "McLean",
    "state": "VA",
    "postalCode": "22102"
}

我正在尝试使用以下代码 post 使用以下 C#

到 api
var request = new ApiRequest {
  business = "Hilton Hotels",
    address = "7930 Jones Branch Drive",
    city = "McLean",
    state = "VA",
    postalCode = "22102",
};

var requestJson = JsonConvert.SerializeObject(request);

var httpRequestMessage = new HttpRequestMessage {
  Method = HttpMethod.Post,
    RequestUri = new Uri("https://zzz-apis-xyz.com/abc/abc-data/v1/abcbusinessdata"),
    Headers = {
      {
        "X-Special-Client-Id",
        "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
      },
      {
        "X-Special-Client-Secret",
        "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
      },
      {
        "clientID",
        "cccccc"
      },
      {
        "abcDistributedKey",
        Guid.NewGuid().ToString()
      },
      {
        "systemName",
        "GLASS"
      },

    },
    Content = new StringContent(requestJson, Encoding.UTF8, "application/json"),

};

using(var client = new HttpClient(new HttpClientHandler {
  UseDefaultCredentials = true
})) {
  client.BaseAddress = new Uri("https://zzz-apis-xyz.com/");

  client.DefaultRequestHeaders.Accept.Clear();
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

  try {
    var response = await client.SendAsync(httpRequestMessage);
    var stringResponse = await response.Content.ReadAsStringAsync();

  } catch (Exception e) {
    Console.WriteLine(e);
    throw;
  }
}
}

此代码在两天前工作,但今天失败并出现错误(另一个开发人员也遇到此问题)(注意:vscode 和此 C# 代码都 运行 在同一台机器上) :

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream. at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at ThirdPartyData.WebApi.Tests.ServiceTest.d__1.MoveNext() in ccccccccccccccc :line 128 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream. at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

您可能需要像这样强制执行 TLS 版本

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

您可以找到有关 SecurityProtocol 的更多信息 here