繁体   English   中英

c# HttpClient 抛出异常消息

[英]c# HttpClient throwing exception messages

我正在尝试使用 .net 框架向外部 API 发送发布请求,但它不断失败并出现一堆错误并抛出一个我似乎无法理解发生了什么的 500。

请求需要两个标头content-typeauthorization

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

正在发送的数据:

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

异常消息:

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

这很可能是 TLS 问题。 如果您的应用程序运行的版本低于 .Net Framework 4.6,则不会自动支持 TLS 1.2。 尝试将其添加到您的应用程序中:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

或者如果您需要支持其他版本:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM