using System; using System.IO; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Collections.Generic; public class HttpUtil { /// <summary> /// 通用http方法 /// </summary> /// <param name="url">请求地址</param> /// <param name="postData">发送的数据</param> /// <param name="encoding">发送的编码格式</param> /// <param name="contentType">内容类型,取值推荐从本工具类的"HttpContentType"中取</param> /// <param name="postType">发送类型</param> /// <param name="headDic">头字典</param> /// <returns></returns> public static string HttpMethod(string url, string postData, Encoding encoding, string contentType, PostType postType, Dictionary<string, string> headDic) { try { if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = postType.ToString(); request.Accept = "*/*"; request.ContentType = contentType; request.Credentials = CredentialCache.DefaultCredentials; //用户标识一定要加,不然有些严一点的网站会返回错误信息,比如说微信的,不按照规范来,很容易403 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)"; //添加请求头 if (headDic != null && headDic.Count > 0) { Dictionary<string, string>.Enumerator it = headDic.GetEnumerator(); while (it.MoveNext()) { request.Headers.Add(it.Current.Key, it.Current.Value); } } if (!string.IsNullOrEmpty(postData)) { byte[] buffer = encoding.GetBytes(postData); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { //这一步很有必要,因为有的地方会把错误信息放在异常里面,但是直接ex.message是看不到具体信息的,只有显示403拒绝访问 response = (HttpWebResponse)ex.Response; using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } } catch (Exception ex) { return ex.Message;//返回错误信息 } } /// <summary> /// 请求类型 /// </summary> public enum PostType { POST, GET } /// <summary> /// HTTP 内容类型(Content-Type) /// </summary> public class HttpContentType { /// <summary> /// 资源类型:普通文本 /// </summary> public const string TEXT_PLAIN = "text/plain"; /// <summary> /// 资源类型:JSON字符串 /// </summary> public const string APPLICATION_JSON = "application/json"; /// <summary> /// 资源类型:未知类型(数据流) /// </summary> public const string APPLICATION_OCTET_STREAM = "application/octet-stream"; /// <summary> /// 资源类型:表单数据(键值对) /// </summary> public const string WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; /// <summary> /// 资源类型:表单数据(键值对)。编码方式为 gb2312 /// </summary> public const string WWW_FORM_URLENCODED_GB2312 = "application/x-www-form-urlencoded;charset=gb2312"; /// <summary> /// 资源类型:表单数据(键值对)。编码方式为 utf-8 /// </summary> public const string WWW_FORM_URLENCODED_UTF8 = "application/x-www-form-urlencoded;charset=utf-8"; /// <summary> /// 资源类型:多分部数据 /// </summary> public const string MULTIPART_FORM_DATA = "multipart/form-data"; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } }