Как преобразовать следующий запрос curl в asp. net c# код
curl -k -s -X POST --data "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><sms type=\"mt\" ><source><address><alphanumeric>7pack</alphanumeric></address></source><destination><address><number type=\"international\">88012345677</number></address></destination><rsr type=\"none\"/><ud type=\"text\" encoding=\"unicode\">Test SMS</ud></sms></message>" -H "Content-Type:text/xml" --cert 7pack.crt:"password" --key 7pack.key -u 7Pack_didm:tt1vTdJO https://www.example.com
Я пробовал следующий код, но он не работает.
var handler = new WebRequestHandler();
handler.ClientCertificates.Add(new X509Certificate2(crtPath, "password"));
var byteArray = Encoding.ASCII.GetBytes("username:password");
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
using (var client = new HttpClient(handler))
{
var data = new StringContent(ArgumentPost2);
client.DefaultRequestHeaders.Authorization = header;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));//ACCEPT header
var response = await client.PostAsync(ArgumentURL, data);
string result = response.Content.ReadAsStringAsync().Result;
}
Это дает следующее исключение
{
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred while sending the request.",
"ExceptionType": "System.Net.Http.HttpRequestException",
"StackTrace": " at WebAPI.Controllers.LogisticsController.<sendSmSAsync>d__29.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__1`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.",
"ExceptionType": "System.Net.WebException",
"StackTrace": " at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)\r\n at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "The remote certificate is invalid according to the validation procedure.",
"ExceptionType": "System.Security.Authentication.AuthenticationException",
"StackTrace": " at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)\r\n at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)\r\n at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)"
}
}
}