zoukankan      html  css  js  c++  java
  • 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);
    

      

      

  • 相关阅读:
    委托和事件
    Entity Framework应用:导航属性
    Entity Framework应用:根据实体的EntityState状态实现增删改查
    Entity Framework应用:使用EF的DataBase First模式实现数据库的增删改查
    Entity Framework应用:EntityFramework DataBase First模式
    dapper支持操作函数和事物
    单元测试
    IOC容器:Unity
    MVC教程九:异常过滤器
    MVC教程八:缓存过滤器
  • 原文地址:https://www.cnblogs.com/haifuma/p/10600136.html
Copyright © 2011-2022 走看看