标签:
直接Copy拿去用吧:
新的 tls 协议需要新的.net版本, tls 至少更新到.net4吧,尽量用最新的.net! 不然出错了就折腾...
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.ServiceModel.Security; using System.Net; using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; namespace Lzp.Service { /// <summary> /// 有关HTTP/HTTPS请求的辅助类 lzpong /// 默认 UTF-8 编码 /// </summary> public class HTTPS { public HTTPS() { spt = SecurityProtocolType.Tls; requestEncoding = Encoding.GetEncoding("UTF-8"); requestUserAgent = DefaultUserAgent; } /// <summary> /// SecurityProtocolType /// 如果https出现"基础连接..."或"ssl/tls..."错误,更改此属性; 也可能需要升级 .net 已支持更新的安全协议 /// </summary> public static SecurityProtocolType spt { get; set; } //设置编码 默认编码是UTF-8 public static Encoding requestEncoding { get; set; } //设置编码 public static void SetEncoding(string endcode) { requestEncoding = Encoding.GetEncoding(endcode); } private static string requestUserAgent = DefaultUserAgent; private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"; //设置浏览器版本 public static void SetUserAgent(string userAgent = null) { if (!String.IsNullOrWhiteSpace(userAgent))requestUserAgent = userAgent; else requestUserAgent = DefaultUserAgent; } /// <summary> /// 创建GET方式的HTTP/HTTPS请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreateGet(string url, int? timeout = null, string userAgent = null, CookieCollection cookies = null) { return Create(url, null, "GET", requestEncoding, timeout, cookies); } /// <summary> /// 创建POST方式的HTTP/HTTPS请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的数据</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreatePost(string url, string parameters = null, int? timeout = null, CookieCollection cookies = null) { return Create(url, parameters, "POST", requestEncoding, timeout, cookies); } /// <summary> /// 创建HTTP/HTTPS请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的参数名称及参数值字典</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse Create(string url, string parameters=null, string method = "GET", Encoding requestEncoding = null, int? timeout = null, CookieCollection cookies = null) { if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url"); if (requestEncoding == null) requestEncoding=Encoding.GetEncoding("UTF-8"); HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.SecurityProtocol = spt; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else request = WebRequest.Create(url) as HttpWebRequest; request.Method = method; request.UserAgent = requestUserAgent; if (timeout.HasValue) request.Timeout = timeout.Value; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //如果需要POST数据 if (method == "POST") { request.ContentType = "application/x-www-form-urlencoded"; if (!String.IsNullOrWhiteSpace(parameters)) { byte[] data = requestEncoding.GetBytes(parameters); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } } return request.GetResponse() as HttpWebResponse; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //总是接受 } } }
当然,有从网上搜罗的, 但是也是自己改装优化过的,so 还是我的咯
转载应注明http://home.cnblogs.com/u/lzpong/
标签:
原文地址:http://www.cnblogs.com/lzpong/p/5545464.html