首页新闻找找看学习计划

微信公众平台SSL加密的问题

0
悬赏园豆:30 [已解决问题] 解决于 2014-11-07 15:56

公司的代码中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();
                }
            }
        }
进击的菜鸟的主页 进击的菜鸟 | 初学一级 | 园豆:105
提问于:2014-11-04 15:09
< >
分享
最佳答案
0

buzhidao

收获园豆:30
龙卷风摧毁停车场! | 初学一级 |园豆:10 | 2014-11-07 14:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册