HttpWebRequest HttpClient 简单封装使用,支持https
HttpWebRequest
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.IO.Compression; 5 using System.Linq; 6 using System.Net; 7 using System.Net.Security; 8 using System.Security.Cryptography.X509Certificates; 9 using System.Text; 10 using TT.Utilities.Extends; 11 12 namespace TT.Utilities.Web 13 { 14 public class HttpRequest 15 { 16 public static HttpWebRequest CreateHttpWebRequest(string url) 17 { 18 HttpWebRequest request; 19 //如果是发送HTTPS请求 20 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) 21 { 22 //ServicePointManager.DefaultConnectionLimit = 1000; 23 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); 24 request = WebRequest.Create(url) as HttpWebRequest; 25 request.ProtocolVersion = HttpVersion.Version11; 26 } 27 else 28 { 29 request = WebRequest.Create(url) as HttpWebRequest; 30 } 31 request.Proxy = null; 32 request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); 33 return request; 34 } 35 36 /// <summary> 37 /// 38 /// </summary> 39 /// <param name="url">url</param> 40 /// <param name="dic">参数</param> 41 /// <param name="headerDic">请求头参数</param> 42 /// <returns></returns> 43 public static string DoPost(string url, Dictionary<string, string> dic, Dictionary<string, string> headerDic) 44 { 45 HttpWebRequest request = CreateHttpWebRequest(url); 46 request.Method = "POST"; 47 request.Accept = "*/*"; 48 request.ContentType = HttpContentTypes.GetContentType(HttpContentTypes.HttpContentTypeEnum.JSON); 49 if (headerDic != null && headerDic.Count > 0) 50 { 51 foreach (var item in headerDic) 52 { 53 request.Headers.Add(item.Key, item.Value); 54 } 55 } 56 if (dic != null && dic.Count > 0) 57 { 58 var jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(dic); 59 byte[] buffer = Encoding.UTF8.GetBytes(jsonStr); 60 request.ContentLength = buffer.Length; 61 try 62 { 63 request.GetRequestStream().Write(buffer, 0, buffer.Length); 64 } 65 catch (WebException ex) 66 { 67 throw ex; 68 } 69 } 70 else 71 { 72 request.ContentLength = 0; 73 } 74 return HttpResponse(request); 75 } 76 77 public static string DoPost(string url, Dictionary<string, string> dic) 78 { 79 return DoPost(url, dic, null); 80 } 81 82 static object olock = new object(); 83 public static string HttpResponse(HttpWebRequest request) 84 { 85 try 86 { 87 //lock (olock) 88 //{ 89 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 90 { 91 var contentEncodeing = response.ContentEncoding.ToLower(); 92 93 if (!contentEncodeing.Contains("gzip") && !contentEncodeing.Contains("deflate")) 94 { 95 using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 96 { 97 return reader.ReadToEnd(); 98 } 99 } 100 else 101 { 102 #region gzip,deflate 压缩解压 103 if (contentEncodeing.Contains("gzip")) 104 { 105 using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)) 106 { 107 using (StreamReader reader = new StreamReader(stream)) 108 { 109 return reader.ReadToEnd(); 110 } 111 } 112 } 113 else //if (contentEncodeing.Contains("deflate")) 114 { 115 using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress)) 116 { 117 using (StreamReader reader = new StreamReader(stream)) 118 { 119 return reader.ReadToEnd(); 120 } 121 } 122 } 123 #endregion gzip,deflate 压缩解压 124 } 125 } 126 //} 127 } 128 catch (WebException ex) 129 { 130 throw ex; 131 } 132 } 133 134 public static string DoGet(string url, Dictionary<string, string> dic) 135 { 136 try 137 { 138 var argStr = dic == null ? "" : dic.ToSortUrlParamString(); 139 argStr = string.IsNullOrEmpty(argStr) ? "" : ("?" + argStr); 140 HttpWebRequest request = CreateHttpWebRequest(url + argStr); 141 request.Method = "GET"; 142 request.ContentType = "application/json"; 143 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 144 { 145 using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 146 { 147 return reader.ReadToEnd(); 148 } 149 } 150 } 151 catch (Exception ex) 152 { 153 throw ex; 154 } 155 } 156 157 public static string DoGet(string url, Dictionary<string, string> dic, Dictionary<string, string> headerDic) 158 { 159 try 160 { 161 var argStr = dic == null ? "" : dic.ToSortUrlParamString(); 162 argStr = string.IsNullOrEmpty(argStr) ? "" : ("?" + argStr); 163 HttpWebRequest request = CreateHttpWebRequest(url + argStr); 164 request.Method = "GET"; 165 request.ContentType = "application/json"; 166 foreach (var item in headerDic) 167 { 168 request.Headers.Add(item.Key, item.Value); 169 } 170 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 171 { 172 using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 173 { 174 return reader.ReadToEnd(); 175 } 176 } 177 } 178 catch (Exception ex) 179 { 180 throw ex; 181 } 182 } 183 184 private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 185 { 186 return true; //总是接受 187 } 188 } 189 }