.NET HTTP通用请求方法get/post

2021-05-03 20:26

阅读:750

标签:isnull   keep   style   cep   exce   erer   false   adt   gzip   

 

.NET HTTP通用请求方法get/post

 

public class HttpHelper
    {
        /// 
        /// 发起Http请求
        /// 
        /// 请求实体
        /// 
        public static HttpReturnDtoobject> DoHttp(HttpRequestDto requestDto)
        {
            var msg = new HttpReturnDtoobject>();
            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;
        }

        /// 
        /// 初始化http请求
        /// 
        /// 
        /// 
        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; }

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

        public string Accept { get; set; }

        public string AcceptEncoding { get; set; }

        /// 
        /// 超时时间(毫秒)默认100000
        /// 
        public int Timeout { get; set; }

        public string UserAgent { get; set; }

        /// 
        /// POST请求时,数据是否进行gzip压缩
        /// 
        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";
        }
    }

    /// 
    /// 返回信息实体
    /// 
    public class HttpRequestDto
    {
        /// 
        /// 请求地址
        /// 
        public string Url { get; set; }
        //请求数据
        public string Data { get; set; }
        /// 
        /// 请求方法post/get
        /// 
        public string Method { get; set; }
        /// 
        /// 请求方法post/get
        /// 
        public HttpConfig HttpConfigDto { get; set; }
    }

    /// 
    /// 返回信息实体
    /// 
    /// 
    public class HttpReturnDto
    {
        public HttpReturnDto()
        {
            IsSuccess = false;
            Message = "操作失败";
        }
        //是否执行成功
        public bool IsSuccess { get; set; }
        //编码
        public string Code { get; set; }
        //信息
        public string Message { get; set; }
        //返回数据
        public T Data { get; set; }
    }

 

.NET HTTP通用请求方法get/post

标签:isnull   keep   style   cep   exce   erer   false   adt   gzip   

原文地址:https://www.cnblogs.com/jiangqw/p/12121230.html


评论


亲,登录后才可以留言!