HttpWebRequest请求Https协议的WebApi

2021-06-10 14:04

阅读:380

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

public static class RequestClient
    {
        /// 
        /// 参数列表转为string
        /// 
        /// 
        /// 
        public static string GetParam(Dictionary 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();
        }

        /// 
        /// 指定Post地址使用Get 方式获取全部字符串
        /// 
        /// 请求后台地址
        /// 
        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


评论


亲,登录后才可以留言!