C# Ignore certificate errors?

后端 未结 11 1142
鱼传尺愫
鱼传尺愫 2020-11-22 15:01

I am getting the following error during a web service request to a remote web service:

Could not establish trust relationship for the SSL/TLS secure c

11条回答
  •  孤城傲影
    2020-11-22 15:34

    This code worked for me. I had to add TLS2 because that's what the URL I am interested in was using.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    ServicePointManager.ServerCertificateValidationCallback +=
        (sender, cert, chain, sslPolicyErrors) => { return true; };
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(UserDataUrl);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new
          MediaTypeWithQualityHeaderValue("application/json"));
        Task response = client.GetStringAsync(UserDataUrl);
        response.Wait();
    
        if (response.Exception != null)
        {
             return null;
        }
    
        return JsonConvert.DeserializeObject(response.Result);
    }
    

提交回复
热议问题