码迷,mamicode.com
首页 > Windows程序 > 详细

C# HttpWebRequest http请求

时间:2019-10-27 11:04:32      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:byte   iso   ror   验证方式   ast   length   http   def   static   

class HttpService
    {
        /// <summary>
        /// 处理http POST请求
        /// </summary>
        /// <param name="url">请求的url地址</param>
        /// <param name="headers">协议标头</param>
        /// <param name="contentType">请求的内容类型</param>
        /// <param name="dataStream">请求的数据流</param>
        /// <param name="timeout">请求的超时时间(秒)</param>
        /// <returns>http POST成功后返回的数据,失败抛异常</returns>
        public static string Post(string url, Headers headers, string contentType, string dataStream, int timeout)
        {
            System.GC.Collect();//垃圾回收,回收没有正常关闭的http链接
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream reqStream = null;
            string result = "";
            try
            {
                //设置最大链接数
                ServicePointManager.DefaultConnectionLimit = 200;
                //设置https验证方式
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                            new RemoteCertificateValidationCallback(CertificateValidation);
                }
                request = (HttpWebRequest)WebRequest.Create(url);

                //HttpWebRequest 相关属性
                request.Method = "POST";
                request.Timeout = timeout * 1000;
                request.ContentType = contentType;
                //配置协议标头
                if (headers != null)
                {
                    request.Headers.Set(headers.Name, headers.Value);
                }                
                byte[] data = System.Text.Encoding.UTF8.GetBytes(dataStream);
                request.ContentLength = data.Length;

                //写入数据
                reqStream = request.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();

                //返回数据
                response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd().Trim();
                sr.Close();
            }
            //处理多线程模式下线程中止
            //catch (System.Threading.ThreadAbortException e)
            //{
            //    System.Threading.Thread.ResetAbort();
            //}
            catch (Exception e)
            {
                throw new HttpServiceException(e.ToString());
            }
            finally
            {
                //关闭连接和流
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return result;
        }
        /// <summary>
        /// 处理http GET请求
        /// </summary>
        /// <param name="url">请求的url地址</param>
        /// <returns></returns>
        public static string Get(string url)
        {
            System.GC.Collect();//垃圾回收,回收没有正常关闭的http链接
            string result = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            try
            {
                //设置最大链接数
                ServicePointManager.DefaultConnectionLimit = 200;
                //设置https验证方式
                if(url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                        new RemoteCertificateValidationCallback(CertificateValidation);
                }
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";

                //返回数据
                response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd().Trim();
                sr.Close();
            }
            //处理多线程模式下线程中止
            //catch (System.Threading.ThreadAbortException e)
            //{
            //    System.Threading.Thread.ResetAbort();
            //}
            catch (Exception e)
            {
                throw new HttpServiceException(e.ToString());
            }
            finally
            {
                //关闭连接和流
                if(response != null)
                {
                    response.Close();
                }
                if(request != null)
                {
                    request.Abort();
                }
            }
            return result;
        }
        /* 忽略证书认证错误
         * .NET的SSL通信过程中,使用的证书可能存在各种问题
         * 此方法可以跳过服务器证书验证,完成正常通信。*/
        private static bool CertificateValidation(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            // 认证正常,没有错误   
            return true;
        }
    }
    /// <summary>
    /// 协议标头
    /// </summary>
    class Headers
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public Headers(string name,string value)
        {
            this.Name = name;
            this.Value = value;
        }
    }
    class HttpServiceException : Exception
    {
        public HttpServiceException(string msg) : base(msg)
        {

        }
    }

 

C# HttpWebRequest http请求

标签:byte   iso   ror   验证方式   ast   length   http   def   static   

原文地址:https://www.cnblogs.com/CaiNongSun/p/11746744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!