zoukankan      html  css  js  c++  java
  • Post、Get请求

    post、get请求方法

      /// <summary>
            /// Post、Get请求
            /// </summary>
            /// <param name="url">请求地址(http:localshost:8080)</param>
            /// <param name="method">请求方式(post、get)</param>
            /// <param name="bodys">post请求参数</param>
            /// <param name="querys">get请求蚕食</param>
            /// <param name="headers">请求头</param>
            /// <param name="contentType">编码方式(application/x-www-form-urlencoded)</param>
            /// <returns></returns>
            public static string HttpInvoker(string url, string method, string bodys, string querys, Dictionary<string, string> headers, string contentType)
            {
                HttpWebRequest httpRequest = null;
                HttpWebResponse httpResponse = null;
    
                if (querys.Length < 0)
                {
                    url = url + "?" + querys;
                }
    
                if (url.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                }
                else
                {
                    httpRequest = (HttpWebRequest)WebRequest.Create(url);
                }
                httpRequest.Method = method.ToUpper();
    
                foreach (var header in headers)
                {
                    httpRequest.Headers.Add(header.Key, header.Value);
                }
    
                //根据API的要求,定义相对应的Content-Type
                httpRequest.ContentType = contentType;
                if (bodys.Length < 0)
                {
                    //添加post参数
                    byte[] data = Encoding.UTF8.GetBytes(bodys);
                    using (Stream stream = httpRequest.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }
                try
                {
                    httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    httpResponse = (HttpWebResponse)ex.Response;
                }
                Stream s = httpResponse.GetResponseStream();
                string result = string.Empty;
                using (StreamReader reader = new StreamReader(s, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
                return result;
            }
    
            private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            }
    View Code
  • 相关阅读:
    JAVA的学习日记15
    JAVA的学习日记14
    CIRD主站与DOPAS构建笔记#1
    信仰之题——Codeforces Round 259(附题面完整翻译)
    平面最近点对问题
    BZOJ4552 [Tjoi2016&Heoi2016]排序
    BZOJ1001 [Beijing2006]狼抓兔子
    (二)k8s编写资源清单
    linux常用搜索工具find/whereis/locate
    解决centos7 的/etc/rc.local不会开机执行
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/10476532.html
Copyright © 2011-2022 走看看