HttpWebRequest请求Https协议的WebApi

Posted haifuma

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpWebRequest请求Https协议的WebApi相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

在每个请求的.net HttpWebRequest上设置SecurityProtocol(Ssl3或TLS)

C# HttpWebRequest GET HTTP HTTPS 请求

HttpWebRequest请求https为啥会出现重复内容

使用 HTTPS 和 httpWebRequest

请求中止无法创建 SSL/TLS 安全通道 - HttpWebRequest

C# 利用HttpWebRequest进行HTTPS的post请求的示例