HTTP 请求
2021-05-15 22:28
标签:catch read agent bre ssl cat contain imu auto public static class HttpUtil #endregion #region cookie容器 /// /// var cookie = HttpContext.Current.Session["CookieContainer98562"] as CookieContainer; return cookie; /// #endregion #region 模拟浏览器请求数据 /// return GetData(req, en); /// if (req.RequestUri.ToString().StartsWith("https", StringComparison.OrdinalIgnoreCase)) var res = (HttpWebResponse)req.GetResponse(); /// System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => return ((HttpWebResponse)req.GetResponse()).GetResponseStream(); public static string PrintCookie(CookieCollection cookies) return s; /// System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => req.BeginGetResponse(callback, req); return PostData(req, postData, Encoding.UTF8); /// /// req.MaximumAutomaticRedirections = 4; var buffer = en.GetBytes(postData); if (req.RequestUri.ToString().StartsWith("https", StringComparison.OrdinalIgnoreCase)) var res = (HttpWebResponse)req.GetResponse(); using (var resStream = res.GetResponseStream()) /// req.ContentLength = buffer.Length; System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => var res = (HttpWebResponse)req.GetResponse(); #endregion #region 填充基本的HttpWebRequest /// if (null == req.CookieContainer) req.KeepAlive = true; /// #endregion #region 获取所有参数 /// #endregion #region 其他方法 /// #endregion HTTP 请求 标签:catch read agent bre ssl cat contain imu auto 原文地址:http://www.cnblogs.com/zy-theone/p/7749944.html
{
#region 变量
/// 创建Cookie容器。
///
///
public static CookieContainer CreateCookie()
{
return new CookieContainer();
}
/// 获取 cookie容器。
///
public static CookieContainer MyCookie
{
get
{
try
{
if (null == HttpContext.Current)
return null;
if (null == cookie)
{
cookie = CreateCookie();
HttpContext.Current.Session["CookieContainer98562"] = cookie;
}
}
catch
{
return null;
}
}
}
/// 获取指定URI下的Cookie键和值。
///
///
///
public static string GetCookeValues(Uri uri)
{
var t = string.Empty;
foreach (Cookie _cookie in HttpUtil.MyCookie.GetCookies(new Uri("http://b2b.cits.com.cn")))
{
t += _cookie.Name + ":" + _cookie.Value + "\n";
}
return t;
}
///
/// 模拟浏览器GET数据。
///
/// 请求网址
///
public static string GetData(string url)
{
return GetData(url, Encoding.UTF8);
}
/// 模拟浏览器GET数据。
///
/// 请求网址
/// 字符编码
///
public static string GetData(string url, Encoding en)
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = MyCookie;
req.KeepAlive = true;
req.Method = "GET";
req.Host = "flights.ctrip.com";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.AllowAutoRedirect = true;
req.Timeout = 50000;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//req.Accept = "application/json";
}
/// 模拟浏览器GET数据。
///
/// 请求网址
/// 编码
///
public static string GetData(HttpWebRequest req, Encoding en)
{
if (null == req.CookieContainer)
req.CookieContainer = MyCookie;
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
req.ProtocolVersion = HttpVersion.Version10;
}
//CookieCollection cook = res.Cookies;
using (var resStream = res.GetResponseStream())
{
using (var sr = new StreamReader(resStream, en))
{
return sr.ReadToEnd();
}
}
}
/// 模拟浏览器下载数据。
///
/// 请求网址
///
public static Stream DownloadData(HttpWebRequest req)
{
if (null == req.CookieContainer)
req.CookieContainer = MyCookie;
{
return true;
};
}
{
var s = "";
foreach (System.Net.Cookie _c in cookies)
s += _c.Name + ":" + _c.Value + "(" + _c.Domain + ")" + ",";
}
/// 模拟浏览器GET数据。
///
/// 请求网址
/// 编码
///
public static void GetDataAsync(HttpWebRequest req, AsyncCallback callback)
{
if (null == req.CookieContainer)
req.CookieContainer = MyCookie;
{
return true;
};
}
///
/// 模拟浏览器请求Post数据。
///
/// 对方API网址
/// 请求数据
///
public static string PostData(string url, string postData)
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = true;
req.Method = "POST";
req.AllowAutoRedirect = true;
req.CookieContainer = MyCookie;
req.Host = "kyfw.12306.cn";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E)";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.AllowAutoRedirect = true;
req.Timeout = 50000;
}
/// 模拟浏览器请求Post数据。
///
/// http请求
/// 请求数据
/// 编码
///
public static string PostData(HttpWebRequest req, string postData, Encoding en)
{
CookieCollection cookies = null;
return PostData(req, postData, en, ref cookies);
}
/// 模拟浏览器请求Post数据。
///
/// http请求
/// 请求数据
/// 编码
/// 从服务器端获取到的关联cookie
///
public static string PostData(HttpWebRequest req, string postData, Encoding en, ref CookieCollection cookies)
{
if (null == req.CookieContainer)
req.CookieContainer = MyCookie;
req.MaximumResponseHeadersLength = 4;
req.Credentials = CredentialCache.DefaultCredentials;
req.ContentLength = buffer.Length;
using (var reqStream = req.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
req.ProtocolVersion = HttpVersion.Version10;
}
if (null != req.CookieContainer)
{
res.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
cookies = res.Cookies;
}
{
using (var sr = new StreamReader(resStream, en))
{
return sr.ReadToEnd();
}
}
}
/// Htpp Post请求数据。
///
/// 对方API网址
/// 请求数据
/// 字符编码
///
public static string PostData(string url, byte[] buffer, Encoding enCoding)
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = true;
req.Method = "POST";
req.AllowAutoRedirect = true;
//req.CookieContainer = MyCookie;
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E)";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.AllowAutoRedirect = true;
req.Timeout = 50000;
using (var reqStream = req.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}
{
return true;
};
using (var resStream = res.GetResponseStream())
{
using (var sr = new StreamReader(resStream, enCoding))
{
return sr.ReadToEnd();
}
}
}
/// 填充基本的HttpWebRequest。
///
/// HttpWebRequest对象
/// 请求方法(取值:GET OR POST)
/// Host标头值
/// 当前引用页面网址
public static void FillWebRequest(HttpWebRequest req, string method, string host, string referer)
{
if (null == req)
return;
req.CookieContainer = HttpUtil.MyCookie;
req.Method = method;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.AllowAutoRedirect = true;
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0";
req.AllowAutoRedirect = true;
req.Timeout = 50000;
req.Host = host;
req.Referer = referer;
}
/// 创建HTTP请求对象。
///
/// Cookie对象
/// 请求网址
/// 请求方法(取值:GET OR POST)
/// Host标头值
/// 当前引用页面网址
///
public static HttpWebRequest CreateWebRequest(CookieContainer cookie, string url, string method, string host, string referer)
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cookie;
FillWebRequest(req, method, host, referer);
return req;
}
/// 获取所有请求参数(GET OR POST)。
///
///
///
public static string GetRequestString(HttpContext context)
{
return context.Request.RequestType.IgnoreCaseEquals("POST") ? context.Request.Form.ToString() : context.Request.QueryString.ToString();
}
/// 验证证书。
///
///
///
///
///
///
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}