.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; } }