Search code examples
c#httpclient

c# HttpClient throwing exception messages


I am trying to send off a post request using .net framework to an external API and it keep failing with a bunch of errors and throwing a 500 that I cant seem to understand whats going on.

The request requires two headers content-type and authorization

 using (var client = new HttpClient())
            {
                try
                {
                    var url = new Uri("https://api.intelepeer.com/_rest/v4/app/sms/send");
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "********************************");

                    var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
                    var response = client.PostAsync(url,  stringContent).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        success = 1;
                    }
                    else
                    {
                        fail = 1;
                        throw new HttpUnhandledException("Call to service failed");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Call to service failed", ex);
                }
            }

Data being sent:

[
  {to: "+11234567890, from: "+13245345533, text: "asdfh hasjdfhas"}
]

Exception Messages:

ExceptionMessage: "Call to service failed"
ExceptionType: "System.Exception"
InnerException: {Message: "An error has occurred.", ExceptionMessage: "One or more errors occurred.",…}
ExceptionMessage: "One or more errors occurred."
ExceptionType: "System.AggregateException"
InnerException: {Message: "An error has occurred.", ExceptionMessage: "An error occurred while sending the request.",…}
ExceptionMessage: "An error occurred while sending the request."
ExceptionType: "System.Net.Http.HttpRequestException"
InnerException: {Message: "An error has occurred.",…}
ExceptionMessage: "The underlying connection was closed: An unexpected error occurred on a send."
ExceptionType: "System.Net.WebException"
InnerException: {Message: "An error has occurred.",…}
ExceptionMessage: "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
ExceptionType: "System.IO.IOException"
InnerException: {Message: "An error has occurred.",…}
ExceptionMessage: "An existing connection was forcibly closed by the remote host"
ExceptionType: "System.Net.Sockets.SocketException"
Message: "An error has occurred."
StackTrace: "   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
↵   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)"
Message: "An error has occurred."
StackTrace: "   at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
↵   at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)"
Message: "An error has occurred."
StackTrace: "   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
↵   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)"
Message: "An error has occurred."
StackTrace: null
Message: "An error has occurred."
StackTrace: "   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
↵   at BabelFish.Business.SendManager.Send(DtoSendRequest request) in C:\$Default\Babel Fish\Main\Source\BabelFish\BabelFish.Business\SendManager.cs:line 52"
Message: "An error has occurred."

Solution

  • This is likely a TLS issue. If your application is running less than .Net Framework 4.6, TLS 1.2 is not automatically supported. Try adding this to your application:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    or if you need to support other versions:

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