我正在尝试向我的Xamarin Forms应用程序上的API发送POST请求,并且我收到一条错误,指示发送请求时没有信任关系。我不明白为什么会这样,以及如何解决它。这是例外:
System.Net.Http.HttpRequestException:发送时发生错误 请求---> System.Net.WebException:错误:TrustFailure(The 验证或解密失败。)---> System.IO.IOException: 身份验证或解密失败。 ---> System.IO.IOException:身份验证或解密失败。 ---> Mono.Security.Protocol.Tls.TlsException:从服务器收到的证书无效。错误代码:0xffffffff800b010f at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (System.IAsyncResult asyncResult)[0x00031] in /Users/builder/jenkins/workspace/xamarin-android/xamarin-android/external/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:430 在Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (System.IAsyncResult ar,System.Boolean ignoreEmpty)[0x00000] in /Users/builder/jenkins/workspace/xamarin-android/xamarin-android/external/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:256 在Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (System.IAsyncResult结果)[0x00071] in /Users/builder/jenkins/workspace/xamarin-android/xamarin-android/external/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:418 ---内部异常堆栈跟踪结束---
导致此异常的代码如下:
MultipartFormDataContent content = new MultipartFormDataContent();
content.Headers.Add("X-Auth-Token", "eb27c17f-8bd6-4b94-bc4f-742e361b4e6a");
content.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(new ByteArrayContent(myByteImage));
try
{
HttpResponseMessage response = await _client.PostAsync("https://10.54.66.160:9000/3/matching/search?list_id=3c9f2623-28be-435f-a49f-4dc29c186809&limit=1", content);
string responseContent = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
throw;
}
答案 0 :(得分:1)
似乎问题解决了将以下代码放在MainActivity.cs
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) =>
{
if (cert != null) System.Diagnostics.Debug.WriteLine(cert);
return true;
};
As @ DanielA.White似乎HttpClient的单声道实现不支持自签名证书。