公司的代码中HTTPS请求和HTTP请求没区别,这次爆出SSL漏洞,微信也调整了自己的安全策略。我想问的是我改成现在的HTTPs请求方法可以了么?下面上代码
public static string HttpRequest(string url, string method, string args, string postData = "", string encoding = "utf-8") { HttpWebRequest _request = null; HttpWebResponse _response = null; StreamReader _reader = null; try { if (url.IndexOf("?") == -1) url = url + "?" + args; else url = url.Substring(0, url.IndexOf("?") - 1) + "?" + args; _request = (HttpWebRequest)WebRequest.Create(url); if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; _request.ProtocolVersion = HttpVersion.Version10; } _request.Method = method; if (method.ToLower() == "post") { using (Stream requestStream = _request.GetRequestStream()) { byte[] postDataBytes = Encoding.GetEncoding(encoding).GetBytes(postData); requestStream.Write(postDataBytes, 0, postDataBytes.Length); } } _response = (HttpWebResponse)_request.GetResponse(); _reader = new StreamReader(_response.GetResponseStream(), Encoding.GetEncoding(encoding)); return _reader.ReadToEnd(); } catch (Exception ex) { throw new Exception("HttpRequest Error:" + ex.Message); } finally { _request = null; if (_response != null) { _response.Close(); _reader.Dispose(); } } }
buzhidao