解决方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static class Util { //由于api的特殊性,需要在我们发出请求前,解决未能建立安全通信通道问题,为这次连接设置安全认证 public static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); return true; } } |
写一个utils类,当你在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class HttpUitls { public static class Util { //由于api的特殊性,需要在我们发出请求前,解决未能建立安全通信通道问题,为这次连接设置安全认证 public static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } private static bool RemoteCertificateValidate( object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // trust any certificate!!! System.Console.WriteLine("Warning, trust any certificate"); return true; } } public static string Get(string Url) { Util.SetCertificatePolicy(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Proxy = null; request.KeepAlive = false; request.Method = "GET"; request.ContentType = "application/json; charset=UTF-8"; request.AutomaticDecompression = DecompressionMethods.GZip; //get请求,返回json文件 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } return retString; } } |
1 |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); |
上面这个代码前添加这个方法 Util.SetCertificatePolicy(); 为本次连接设置证书。 from:https://blog.csdn.net/quentien/article/details/122326973
View Details