首页 > 其他分享 >GET/POST后台请求性能优化

GET/POST后台请求性能优化

时间:2023-01-17 14:34:37浏览次数:39  
标签:false string GET request ServicePointManager posturl 后台 POST null

 

 public static string DoGET(string posturl, Hashtable values = null)
        {
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;

            // 准备请求...
            try
            {
                if (posturl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    //这里设置了协议类型
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | (SecurityProtocolType)3072 | SecurityProtocolType.Tls;
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                    ServicePointManager.CheckCertificateRevocationList = false;
                    ServicePointManager.DefaultConnectionLimit = 512;
                    ServicePointManager.Expect100Continue = false;

                    request = WebRequest.Create(posturl) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                    request.KeepAlive = false;
                    request.ServicePoint.Expect100Continue = false;
                    request.ServicePoint.UseNagleAlgorithm = false;
                    request.ServicePoint.ConnectionLimit = 65535;
                    request.AllowWriteStreamBuffering = false;
                }
                else
                {
                    // 设置参数
                    request = WebRequest.Create(posturl) as HttpWebRequest;
                }
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码
                string content = sr.ReadToEnd();

                return content;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

 

  /// <summary>
        /// POST请求处理
        /// </summary>
        /// <param name="posturl"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        public static string DoPOST(string posturl, string postData)
        {
            string content = "";
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...
            try
            {
                if (posturl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    //这里设置了协议类型
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | (SecurityProtocolType)3072 | SecurityProtocolType.Tls;
                    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
                    ServicePointManager.CheckCertificateRevocationList = false;
                    ServicePointManager.DefaultConnectionLimit = 512;
                    ServicePointManager.Expect100Continue = false;

                    request = WebRequest.Create(posturl) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;
                    request.KeepAlive = false;
                    request.ServicePoint.Expect100Continue = false;
                    request.ServicePoint.UseNagleAlgorithm = false;
                    request.ServicePoint.ConnectionLimit = 65535;
                    request.AllowWriteStreamBuffering = false;
                }
                else
                {
                    request = WebRequest.Create(posturl) as HttpWebRequest;
                }

                // 设置参数
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码
                content = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                content = ex.Message;
            }
            return content;
        }

 

标签:false,string,GET,request,ServicePointManager,posturl,后台,POST,null
From: https://www.cnblogs.com/Violety/p/17057726.html

相关文章