【问题标题】:Xamarin.Forms HTTPS and Self Signed Certificate IssueXamarin.Forms HTTPS 和自签名证书问题
【发布时间】:2018-01-10 21:35:04
【问题描述】:

我正在使用 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:发送时出错 请求。 ---> System.Runtime.InteropServices.COMException: 找不到与此错误代码相关的文本。

证书颁发机构无效或不正确

在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Net.Http.HttpHandlerToFilter.d__4.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Net.Http.HttpClientHandler.d__86.MoveNext() --- 内部异常堆栈跟踪结束 --- 在 System.Net.Http.HttpClientHandler.d__86.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Net.Http.HttpClient.d__58.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 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.TaskAwaiter1.GetResult() 在 SampleApp.Views.LoginPage.d__1.MoveNext()

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

【问题讨论】:

  • 你介意显示你的网址吗?
  • 类似于xx.xxx.xxx.xx/v1/login。无法显示完整的 URL 抱歉。以 https:// 开头。
  • 我想你可以使用DependencyService通过Windows.Web.Http独立实现uwp客户端项目中的登录功能。
  • 让我试试看。谢谢。
  • 鞑靼人,你有没有找到绕过自签名证书的解决方案?

标签: c# ssl xamarin https xamarin.forms


【解决方案1】:

此代码有助于解决 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;
   }

【讨论】:

    【解决方案2】:

    使用自签名证书的解决方法是在初始化 HttpClient 之前插入以下代码,以忽略 SSL 证书错误:

    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
    

    确保包含 System.Net。

    希望这会有所帮助。

    【讨论】:

    最近更新 更多