General Method .NET HTTP request get / post

 

General Method .NET HTTP request get / post

 

public class HttpHelper
    {
        /// <summary>
        /// 发起Http请求
        /// </summary>
        /// <param name="requestDto">请求实体</param>
        /// <returns></returns>
        public static HttpReturnDto<object> DoHttp(HttpRequestDto requestDto)
        {
            var msg = new HttpReturnDto<object>();
            if (requestDto == null || string.IsNullOrEmpty(requestDto.Url) || string.IsNullOrEmpty(requestDto.Method))
            {
                msg.IsSuccess = false;
                msg.Message = "请求参数没有全部提供";
                return msg;
            }
            HttpWebRequest httpRequest = GetHttpRequest(requestDto);
            try
            {
                if (httpRequest != null)
                {
                    var response = (HttpWebResponse)httpRequest.GetResponse();
                    var streamIn = response.GetResponseStream();
                    if (streamIn != null)
                    {
                        var reader = new StreamReader(streamIn);
                        msg.Data = reader.ReadToEnd();
                        reader.Close();
                        streamIn.Close();
                        response.Close();

                        msg.IsSuccess = true;
                        msg.Message = "执行成功";
                    }
                }
            }
            catch (Exception ex)
            {
                msg.IsSuccess = false;
                msg.Message = ex.Message;
                return msg;
            }
            return msg;
        }

        /// <summary>
        /// 初始化http请求
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        private static HttpWebRequest GetHttpRequest(HttpRequestDto requestDto)
        {
            var config = requestDto.HttpConfigDto ?? new HttpConfig();
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(requestDto.Url);
            httpRequest.Method = requestDto.Method;
            httpRequest.Referer = config.Referer;
            //有些页面不设置用户代理信息则会抓取不到内容
            httpRequest.UserAgent = config.UserAgent;
            httpRequest.Timeout = config.Timeout;
            httpRequest.Accept = config.Accept;
            httpRequest.Headers.Set("Accept-Encoding", config.AcceptEncoding);
            httpRequest.ContentType = config.ContentType;
            httpRequest.KeepAlive = config.KeepAlive;

            switch (requestDto.Method.ToUpper())
            {
                case "POST":
                    requestDto.Data = requestDto.Data ?? "";
                    var bData = Encoding.UTF8.GetBytes(requestDto.Data);
                    httpRequest.ContentType = "application/xml;charset=utf-8";
                    httpRequest.ContentLength = bData.Length;
                    var streamOut = httpRequest.GetRequestStream();
                    streamOut.Write(bData, 0, bData.Length);
                    streamOut.Close();
                    break;
            }
            return httpRequest;
        }
    }


    public class HttpConfig
    {
        public string Referer { get; set; }

        /// <summary>
        /// 默认(text/html)
        /// </summary>
        public string ContentType { get; set; }

        public string{The Accept GET ; SET ;} 

        public  String AcceptEncoding { GET ; SET ;} 

        ///  <Summary> 
        /// timeout (ms) Default 100000
         ///  </ Summary> 
        public  int the Timeout { GET ; SET ;} 

        public  String {UserAgent GET ; SET ;} 

        ///  <Summary> 
        /// a POST request, whether the data gzip compression
         ///  </ Summary> 
        public  BOOL GZipCompress { GET ; SET; }

        public bool KeepAlive { get; set; }

        public string CharacterSet { get; set; }

        public HttpConfig()
        {
            this.Timeout = 2100000000;
            this.ContentType = "text/html; charset=" + Encoding.UTF8.WebName;
            this.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
            this.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            this.AcceptEncoding = "gzip,deflate";
            this.GZipCompress = false;
            this.KeepAlive = true;
            this.CharacterSet = "UTF-8";
        }
    }

    /// <summary>
    /// 返回信息实体
    /// </summary>
    public class HttpRequestDto
    {
        /// <summary>
        ///Request address
         ///  </ Summary> 
        public  String the Url { GET ; SET ;}
         // request data 
        public  String the Data { GET ; SET ;}
         ///  <Summary> 
        /// request method POST / GET
         ///  </ Summary> 
        public  String method, { GET ; SET ;}
         ///  <Summary> 
        /// request method POST / GET
         ///  </ Summary> 
        public HttpConfig HttpConfigDto { GET ; SET;} 
    } 

    ///  <Summary> 
    /// Returns information entity
     ///  </ Summary> 
    ///  <typeParam name = "T"> </ typeParam> 
    public  class HttpReturnDto <T> 
    { 
        public HttpReturnDto () 
        { 
            isSuccess = to false ; 
            the Message = " operation failed " ; 
        } 
        // whether successful 
        public  BOOL isSuccess { GET ; SET ;}
         // encoding 
        public  String Code { GET; SET ;}
         // information 
        public  String the Message { GET ; SET ;}
         // returns the data 
        public T {the Data GET ; SET ;} 
    }

 

Guess you like

Origin www.cnblogs.com/jiangqw/p/12121230.html
Recommended