c# - HttpClient 发布到 api,获取 System.IO.IOException : Authentication failed because the remote party has closed the transport stream

标签 c# dotnet-httpclient

我正在使用 VS RestClient 插件成功调用服务(为保护 secret 而进行的更改)

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"
}
我正在尝试使用以下代码将其发布到使用以下 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 的更多信息.

关于c# - HttpClient 发布到 api,获取 System.IO.IOException : Authentication failed because the remote party has closed the transport stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68716516/

相关文章:

c# - NLog 扩展以编程方式添加程序集

c# - 什么是 Java 等同于 C# 中的打印值?

c# - System.Net.Http.MultipartFormDataContent 的 "curl -F"参数是否等效?

c# - 使用 Windows 身份验证的 HttpClient 非常慢

C# : Creating a Single Bot Service to Support Multiple Bot Applications

c# - 使用 c# 的亚马逊市场网络服务 (amazon mws)

c# - where 子句中的 IN 运算符

c# - 如何为 HttpClient 调用配置网络跟踪 Dotnet 核心?

c# - 如何在 Blazor 服务器应用程序的服务注册中将访问 token 注入(inject)到 HttpClient?

c# - HttpClient 没有正确序列化 XML