using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using System.Web; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; namespace cuidian.OpenApi.Http { public sealed class WebUtils { private int _timeout = 100000; /// /// 请求与响应的超时时间 /// public int Timeout { get { return this._timeout; } set { this._timeout = value; } } /// /// 执行HTTP POST请求。 /// /// 请求地址 /// 请求参数 /// HTTP响应 public HttpWebResponse Post(string url, IDictionary parameters, string data) { if (parameters != null && parameters.Count > 0) { if (url.Contains("?")) { url = url + "&" + BuildQuery(parameters); } else { url = url + "?" + BuildQuery(parameters); } } HttpWebRequest req = GetWebRequest(url, "POST"); req.ContentType = "application/json;charset=utf-8"; byte[] postData = Encoding.UTF8.GetBytes(data); System.IO.Stream reqStream = req.GetRequestStream(); reqStream.Write(postData, 0, postData.Length); reqStream.Close(); return (HttpWebResponse)req.GetResponse(); } /// /// 执行HTTP GET请求。 /// /// 请求地址 /// 请求参数 /// HTTP响应 public HttpWebResponse Get(string url, IDictionary parameters) { if (parameters != null && parameters.Count > 0) { if (url.Contains("?")) { url = url + "&" + BuildQuery(parameters); } else { url = url + "?" + BuildQuery(parameters); } } Console.WriteLine("url:" + url); HttpWebRequest req = GetWebRequest(url, "GET"); req.ContentType = "application/json;charset=utf-8"; return (HttpWebResponse)req.GetResponse(); } public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { //直接确认,否则打不开 return true; } public HttpWebRequest GetWebRequest(string url, string method) { HttpWebRequest req = null; if (url.Contains("https")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url)); } else { req = (HttpWebRequest)WebRequest.Create(url); } req.ServicePoint.Expect100Continue = false; req.Method = method; req.KeepAlive = true; req.UserAgent = "openapi4net:open.yonyouup.com"; req.Timeout = this._timeout; return req; } /// /// 把响应流转换为文本。 /// /// 响应流对象 /// 编码方式 /// 响应文本 public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding) { System.IO.Stream stream = null; StreamReader reader = null; try { // 以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream(); reader = new StreamReader(stream, encoding); return reader.ReadToEnd(); } finally { // 释放资源 if (reader != null) reader.Close(); if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } /// /// 组装GET请求URL。 /// /// 请求地址 /// 请求参数 /// 带参数的GET请求URL public string BuildGetUrl(string url, IDictionary parameters) { if (parameters != null && parameters.Count > 0) { if (url.Contains("?")) { url = url + "&" + BuildQuery(parameters); } else { url = url + "?" + BuildQuery(parameters); } } return url; } /// /// 组装普通文本请求参数。 /// /// Key-Value形式请求参数字典 /// URL编码后的请求数据 public static string BuildQuery(IDictionary parameters) { StringBuilder postData = new StringBuilder(); bool hasParam = false; IEnumerator> dem = parameters.GetEnumerator(); while (dem.MoveNext()) { string name = dem.Current.Key; string value = dem.Current.Value; // 忽略参数名或参数值为空的参数 if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value)) { if (hasParam) { postData.Append("&"); } postData.Append(name); postData.Append("="); postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8)); hasParam = true; } } return postData.ToString(); } } }