HTTP 请求

2021-05-15 22:28

阅读:442

标签:catch   read   agent   bre   ssl   cat   contain   imu   auto   

public static class HttpUtil
{
#region 变量

#endregion

#region cookie容器

///


/// 创建Cookie容器。
///

///
public static CookieContainer CreateCookie()
{
return new CookieContainer();
}

///


/// 获取 cookie容器。
///

public static CookieContainer MyCookie
{
get
{
try
{
if (null == HttpContext.Current)
return null;

var cookie = HttpContext.Current.Session["CookieContainer98562"] as CookieContainer;
if (null == cookie)
{
cookie = CreateCookie();
HttpContext.Current.Session["CookieContainer98562"] = cookie;
}

return 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;
}

#endregion

#region 模拟浏览器请求数据
///


/// 模拟浏览器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";

return GetData(req, en);
}

///


/// 模拟浏览器GET数据。
///

/// 请求网址
/// 编码
///
public static string GetData(HttpWebRequest req, Encoding en)
{
if (null == req.CookieContainer)
req.CookieContainer = MyCookie;

if (req.RequestUri.ToString().StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
req.ProtocolVersion = HttpVersion.Version10;
}

var res = (HttpWebResponse)req.GetResponse();
//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;

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};

return ((HttpWebResponse)req.GetResponse()).GetResponseStream();
}

public static string PrintCookie(CookieCollection cookies)
{
var s = "";
foreach (System.Net.Cookie _c in cookies)
s += _c.Name + ":" + _c.Value + "(" + _c.Domain + ")" + ",";

return s;
}

///


/// 模拟浏览器GET数据。
///

/// 请求网址
/// 编码
///
public static void GetDataAsync(HttpWebRequest req, AsyncCallback callback)
{
if (null == req.CookieContainer)
req.CookieContainer = MyCookie;

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};

req.BeginGetResponse(callback, req);
}


///


/// 模拟浏览器请求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;

return PostData(req, postData, Encoding.UTF8);
}

///


/// 模拟浏览器请求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.MaximumAutomaticRedirections = 4;
req.MaximumResponseHeadersLength = 4;
req.Credentials = CredentialCache.DefaultCredentials;

var buffer = en.GetBytes(postData);
req.ContentLength = buffer.Length;
using (var reqStream = req.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}

if (req.RequestUri.ToString().StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
req.ProtocolVersion = HttpVersion.Version10;
}

var res = (HttpWebResponse)req.GetResponse();
if (null != req.CookieContainer)
{
res.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
cookies = res.Cookies;
}

using (var resStream = res.GetResponseStream())
{
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;

req.ContentLength = buffer.Length;
using (var reqStream = req.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{
return true;
};

var res = (HttpWebResponse)req.GetResponse();
using (var resStream = res.GetResponseStream())
{
using (var sr = new StreamReader(resStream, enCoding))
{
return sr.ReadToEnd();
}
}
}

#endregion

#region 填充基本的HttpWebRequest

///


/// 填充基本的HttpWebRequest。
///

/// HttpWebRequest对象
/// 请求方法(取值:GET OR POST)
/// Host标头值
/// 当前引用页面网址
public static void FillWebRequest(HttpWebRequest req, string method, string host, string referer)
{
if (null == req)
return;

if (null == req.CookieContainer)
req.CookieContainer = HttpUtil.MyCookie;

req.KeepAlive = true;
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;
}

#endregion

#region 获取所有参数

///


/// 获取所有请求参数(GET OR POST)。
///

///
///
public static string GetRequestString(HttpContext context)
{
return context.Request.RequestType.IgnoreCaseEquals("POST") ? context.Request.Form.ToString() : context.Request.QueryString.ToString();
}

#endregion

#region 其他方法

///


/// 验证证书。
///

///
///
///
///
///
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}

#endregion
}

HTTP 请求

标签:catch   read   agent   bre   ssl   cat   contain   imu   auto   

原文地址:http://www.cnblogs.com/zy-theone/p/7749944.html


评论


亲,登录后才可以留言!