1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Net; 6 using System.Net.Security; 7 using System.Security.Cryptography.X509Certificates; 8 using System.Text; 9 using System.Web; 10 11 namespace SAO.Web.MyWebRequest 12 { 13 public class HttpHelper 14 { 15 public static string ticket { get; set; } //该属性是用来获取令牌用的 16 17 18 /// <summary> 19 /// 获取请求结果 20 /// </summary> 21 /// <returns></returns> 22 public static string GetHttpResult(HttpWebResponse response) 23 { 24 string result = string.Empty; 25 using (Stream stream=response.GetResponseStream()) 26 { 27 using (StreamReader reader=new StreamReader(stream,Encoding.UTF8)) 28 { 29 result = reader.ReadToEnd(); 30 } 31 } 32 return result; 33 } 34 35 /// <summary> 36 /// 创建GET方式的HTTP请求 37 /// </summary> 38 public static HttpWebResponse GetHttpResponse(string url, int timeout, string userAgent, CookieCollection cookies) 39 { 40 HttpWebRequest request = null; 41 request.Method = "GET"; 42 43 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) 44 { 45 //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true) 46 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); 47 request = WebRequest.Create(url) as HttpWebRequest; 48 request.ProtocolVersion = HttpVersion.Version10; //http版本,默认是1.1,这里设置为1.0 49 } 50 else 51 { 52 request = WebRequest.Create(url) as HttpWebRequest; 53 } 54 55 //设置代理UserAgent和超时 56 request.Accept = ""; 57 request.Timeout = timeout; 58 request.UserAgent = userAgent; 59 60 //注:如果要调用WebApi,则需要在请求头增加一个属性来进行Basic认证 61 request.Headers.Add(HttpRequestHeader.Authorization, "BasicAuth " + ticket); 62 63 if (cookies != null) 64 { 65 request.CookieContainer = new CookieContainer(); 66 request.CookieContainer.Add(cookies); 67 } 68 return request.GetResponse() as HttpWebResponse; 69 } 70 71 /// <summary> 72 /// 创建POST方式的HTTP请求 73 /// </summary> 74 public static HttpWebResponse PostHttpResponse(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieCollection cookies) 75 { 76 HttpWebRequest request = null; 77 //如果是发送HTTPS请求 78 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) 79 { 80 //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); 81 request = WebRequest.Create(url) as HttpWebRequest; 82 //request.ProtocolVersion = HttpVersion.Version10; 83 } 84 else 85 { 86 request = WebRequest.Create(url) as HttpWebRequest; 87 } 88 request.Method = "POST"; 89 request.ContentType = "application/x-www-form-urlencoded"; 90 91 //设置代理UserAgent和超时 92 request.UserAgent = userAgent; 93 request.Timeout = timeout; 94 95 if (cookies != null) 96 { 97 request.CookieContainer = new CookieContainer(); 98 request.CookieContainer.Add(cookies); 99 } 100 //发送POST数据 101 if (!(parameters == null || parameters.Count == 0)) 102 { 103 StringBuilder buffer = new StringBuilder(); 104 int i = 0; 105 foreach (string key in parameters.Keys) 106 { 107 if (i > 0) 108 { 109 buffer.AppendFormat("&{0}={1}", key, parameters[key]); 110 } 111 else 112 { 113 buffer.AppendFormat("{0}={1}", key, parameters[key]); 114 i++; 115 } 116 } 117 byte[] data = Encoding.ASCII.GetBytes(buffer.ToString()); 118 using (Stream stream = request.GetRequestStream()) 119 { 120 stream.Write(data, 0, data.Length); 121 } 122 } 123 string[] values = request.Headers.GetValues("Content-Type"); 124 return request.GetResponse() as HttpWebResponse; 125 } 126 127 128 /// <summary> 129 /// 验证证书 130 /// </summary> 131 private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 132 { 133 if (errors == SslPolicyErrors.None) 134 return true; 135 return false; 136 } 137 } 138 }