wanghongsha 2019-12-30
.NET HTTP通用请求方法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 Accept { get; set; } public string AcceptEncoding { get; set; } /// <summary> /// 超时时间(毫秒)默认100000 /// </summary> public int Timeout { get; set; } public string UserAgent { get; set; } /// <summary> /// POST请求时,数据是否进行gzip压缩 /// </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> /// 请求地址 /// </summary> public string Url { get; set; } //请求数据 public string Data { get; set; } /// <summary> /// 请求方法post/get /// </summary> public string Method { get; set; } /// <summary> /// 请求方法post/get /// </summary> public HttpConfig HttpConfigDto { get; set; } } /// <summary> /// 返回信息实体 /// </summary> /// <typeparam name="T"></typeparam> public class HttpReturnDto<T> { 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; } }