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

时间:2017-08-03 10:24:37

标签: c# ssl xamarin https xamarin.forms

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

     

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

     

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

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

2 个答案:

答案 0 :(得分:1)

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

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

确保包含System.Net。

希望这有帮助。

答案 1 :(得分:0)

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