Search code examples
c#dotnet-httpclient

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


I am successfully calling a service using the VS RestClient addin (changes made to protect secrets)

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"
}

I am trying to use the following code to post this to the api using the following C#

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;
  }
}
}

This code worked on two days ago but today fails with the error (another dev is also experiencing this) (NOTE: vscode and this C# code are both running on the same machine):

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()


Solution

  • You may need to enforce the TLS version something like this

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

    You could find some more information about SecurityProtocol here.