我正在尝试使用.net框架向外部API发送发布请求,但该请求始终会因出现一系列错误而失败,并抛出500个我似乎无法理解的错误。
该请求需要两个标头content-type
和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);
}
}
正在发送的数据:
[
{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."
答案 0 :(得分:1)
这可能是TLS问题。如果您的应用程序运行的版本少于.Net Framework 4.6,则不会自动支持TLS 1.2。尝试将其添加到您的应用程序中:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
或者如果您需要支持其他版本:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;