zoukankan      html  css  js  c++  java
  • HTTP请求

    /// <summary>
            /// 提交数据请求
            /// </summary>
            /// <param name="POSTURL">请求提交的地址 如:https://car.etaiping.com:6004/ecms2/portal/商户号/请求类型(接口名称)
            /// <param name="PostData">提交的数据(字符串)</param>
            /// <returns></returns>
            public string RequestData(string PostData)
            {
                string POSTURL = string.Format("https://www.sina.com.cn");
    
                Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                //将提交的字符串数据转换成字节数组
                //注意提交的编码,这里默认的是Default:系统当前编码
                byte[] postData = myEncoding.GetBytes(PostData);
                //设置提交的相关参数 
                HttpWebRequest request = WebRequest.Create(POSTURL) as HttpWebRequest;
    
                //实例化一个证书
                //X509Certificate2 cerCaiShang = GetCertificate();
                ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                //request.ClientCertificates.Add(cerCaiShang);
                request.Method = "POST";
                request.KeepAlive = true;
                //request.AllowAutoRedirect = true;
                request.ProtocolVersion = HttpVersion.Version10;
                request.ContentType = "text/xml;charset=utf-8";
                //request.ContentType = "application/x-www-form-urlencoded";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.ContentLength = postData.Length;
                //request.ClientCertificates.Add(new X509Certificate("C:\CerPath\_Cer.cer"));//证书路径
                
    
                #region 传输数据
                try
                {
                    using (Stream writer = request.GetRequestStream())
                    {
                        writer.Write(postData, 0, postData.Length);
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
    
                #endregion
    
                #region 接收返回的数据
                string srcString = string.Empty;
                try
                {
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    using (Stream sr = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(sr, myEncoding))
                        {
                            srcString = reader.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
                #endregion
                return srcString;
            }
    

      

  • 相关阅读:
    网站安全:你面临2个至关重要的挑战!
    一个只需要点 「下一步」就完成监控 Windows
    论MOBA类游戏五号位的重要性
    CloudTest 事务监控:千呼万唤始出来
    深入浅出 ES6:ES6 与 Babel / Broccoli 的联用
    精华阅读第7期|程序员职业人生规划的三点建议
    (12)打鸡儿教你Vue.js
    (11)打鸡儿教你Vue.js
    (10)打鸡儿教你Vue.js
    (9)打鸡儿教你Vue.js
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/5724888.html
Copyright © 2011-2022 走看看