gpt4 book ai didi

c# - Xamarin.Forms HTTPS 和自签名证书问题

转载 作者:行者123 更新时间:2023-11-30 17:32:51 24 4
gpt4 key购买 nike

我正在使用 Xamarin.Forms,我的首要任务是 UWP。我正在尝试通过 System.Net.Http.HttpClient 发出发布请求我的代码看起来像这样

public async Task<LoginResponse> Login(User user)
{
HttpClient client = await GetClient();

var response = await client.PostAsync(Url, new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json"));
var mobileResult = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<LoginResponse>(mobileResult);

return result;
}

当我发出请求时,我收到了这个错误

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Runtime.InteropServices.COMException: The text associated with this error code could not be found.

The certificate authority is invalid or incorrect

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpHandlerToFilter.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClientHandler.d__86.MoveNext() --- End of inner exception stack trace --- at System.Net.Http.HttpClientHandler.d__86.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClient.d__58.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at SampleApp.Services.LoginService.<Login>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task) at
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() at SampleApp.Views.LoginPage.d__1.MoveNext()

我认为自签名 SSL 导致了问题。我知道我可以使用 Windows.Web HttpClient 来忽略 SSL 错误,但由于某些问题,现在不可能了。我怎么解决这个问题 ?提前致谢。

最佳答案

此代码有助于解决 Xamarin-Windows (UWP) - Windows 8.1 中的以下错误

“找不到与此错误代码关联的文本。”

  public interface IServerCommunication
{
Task<string> GetFromServerAsync(string URL);
}

//稍后,当我下载数据时:(URL 是提供的字符串)

  string result = await DependencyService.Get<IServerCommunication> 
().GetFromServerAsync(URL);

public async Task<string> GetFromServerAsync(string URL)
{
HttpClient client = await PreparedClientAsync();

HttpResponseMessage response;

try
{
response = await client.GetAsync(new Uri(URL));

IBuffer buffer = await response.Content.ReadAsBufferAsync();
DataReader reader = DataReader.FromBuffer(buffer);
byte[] fileContent = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
string result = Encoding.UTF8.GetString(fileContent, 0, fileContent.Length);

return result;
}
catch (Exception ex)
{
return "error";
}
}


private async Task<HttpClient> PreparedClientAsync()
{
var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();

filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);

HttpClient client = new HttpClient(filter);

//I also handle other stuff here (client certificate, authentification), but the
lines above should allow the Httpclient to accept all certificates

return client;
}

关于c# - Xamarin.Forms HTTPS 和自签名证书问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45481509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com