码迷,mamicode.com
首页 > Windows程序 > 详细

HttpWebRequest请求Https协议的WebApi

时间:2019-03-26 15:27:17      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:pve   eth   接受   ring   utf8   summary   reg   字符   pen   

public static class RequestClient
    {
        /// <summary>
        /// 参数列表转为string
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string GetParam(Dictionary<string, string> dic)
        {
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }

            return builder.ToString();
        }

        /// <summary>
        /// 指定Post地址使用Get 方式获取全部字符串
        /// </summary>
        /// <param name="url">请求后台地址</param>
        /// <returns></returns>
        public static string Post(string url, string paramString, string token)
        {
            string result = "";
            //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebRequest request = null;
            //如果是发送HTTPS请求  
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3;
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;
         
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }


            request.Method = "POST";
            request.Headers.Add("Authorization", token);
            request.ContentType = "application/x-www-form-urlencoded";
            #region 添加Post 参数

            byte[] data = Encoding.UTF8.GetBytes(paramString);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受  
        }
    }
                string paramString = RequestClient.GetParam(dic);

                var result = RequestClient.Post(url, paramString, encryptToken);

  

  

HttpWebRequest请求Https协议的WebApi

标签:pve   eth   接受   ring   utf8   summary   reg   字符   pen   

原文地址:https://www.cnblogs.com/haifuma/p/10600136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!