http://www.cnblogs.com/NoRoad/archive/2011/01/27/1945912.html
using System;
using System.Collections.Generic;
using System.Text;using System.Net;using System.IO;
using System.Net.Cache;using System.Drawing;
using System.Text.Regulars;
using System.Runtime.InteropServices;
namespace Better.AvhBooking.BookingInterface
{
/// <summary>
/// 訪問網(wǎng)絡的方法
/// </summary>
public enum HttpRequestType
{
/// <summary>
/// Post請求
/// </summary>
POST,
/// <summary>
/// Get請求
/// </summary>
GET
}
///<summary>
/// 獲取Cookie的方法類。
///</summary>
public class CookieManger
{
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookie(string url, string cookieName, StringBuilder cookieData, ref int size);
[DllImport("Kernel32.dll", SetLastError = true)]
private static extern int GetLastError();
public static CookieContainer GetUriCookieContainer(Uri uri)
{
CookieContainer cookies = null;
//定義Cookie數(shù)據(jù)的大小。
int datasize = 256;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookie(uri.ToString(), null, cookieData, ref datasize))
{
int errCode = GetLastError();
if (datasize < 0)
return null; // 確信有足夠大的空間來容納Cookie數(shù)據(jù)。
cookieData = new StringBuilder(datasize);
if (!InternetGetCookie(uri.ToString(), null, cookieData, ref datasize))
{
errCode = GetLastError();
return null;
}
}
if (cookieData.Length > 0)
{
cookies = new CookieContainer();
string[] cooks = cookieData.ToString().Split(';');
for (int i = 0; i < cooks.Length; i++)
{
if (cooks[i].IndexOf(',') == -1)
cookies.SetCookies(uri, cooks[i]);
}
}
return cookies;
}
public static string GetCookiesString(CookieContainer cookies, Uri uri)
{
if (cookies == null || uri == null)
return "";
CookieCollection cc = cookies.GetCookies(uri);
string szCookies = "";
foreach (Cookie cook in cc)
{
szCookies = szCookies + cook.Name + "=" + cook.Value + ";";
}
return szCookies;
}
}
public static class Http
{
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
/// <summary>
/// 以Post請求獲取一個頁面
/// </summary>
/// <param name="target">目標地址</param>
/// <param name="refere">請求源</param>
/// <param name="cookie">Cookie</param>
/// <param name="parameters">參數(shù)列表</param>
/// <returns></returns>
public static string GetPostHtml(string target, string refere, ref string cookie, params string[] parameters)
{
return GetHtml(target, refere, ref cookie, HttpRequestType.POST, Encoding.GetEncoding("gb2312"), 60, false, parameters);
}
public static string GetPostHtmlPar(string target, string refere, ref string cookie, string parameters)
{
string[] args = null;
if (parameters != null)
args = parameters.Split(new string[] { "&" }, StringSplitOptions.None);
return GetPostHtml(target, refere, ref cookie, args);
}
public static string GetHtmlPar(string target, string refere, ref string cookie, string parameters)
{
string[] args = null;
if (parameters != null)
args = parameters.Split(new string[] { "&" }, StringSplitOptions.None);
return GetHtml(target, refere, ref cookie, HttpRequestType.GET, Encoding.GetEncoding("gb2312"), 60, false, args);
}
/// <summary>
/// 以Get請求獲取一個頁面
/// </summary>
/// <param name="target">目標地址</param>
/// <param name="refere">請求源</param>
/// <param name="cookie">Cookie</param>
/// <param name="parameters">參數(shù)列表</param>
/// <returns></returns>
public static string GetHtml(string target, string refere, ref string cookie, params string[] parameters)
{
return GetHtml(target, refere, ref cookie, HttpRequestType.GET, Encoding.GetEncoding("gb2312"), 60, false, parameters);
}
/// <summary>
/// 獲取一個Stream
/// </summary>
/// <param name="target">目標地址</param>
/// <param name="refere">請求源</param>
/// <param name="cookie">Cookie</param>
/// <param name="parameters">參數(shù)列表</param>
/// <param name="encoding">編碼</param>
/// <param name="timeOut">超時時間</param>
/// <param name="keepAlive">是否建立持續(xù)連接</param>
/// <param name="method">請求類型</param>
/// <returns></returns>
public static Stream GetStream(string target, string refere, ref string cookie, HttpRequestType method, Encoding encoding, int timeOut, bool keepAlive, params string[] parameters)
{
HttpWebResponse response = null;
Stream responseStream = null;
Stream returnStream = null;
try
{
string ps = "";
if (parameters != null && parameters.Length >= 1) { ps = string.Join("&", parameters); }
byte[] bytes = encoding.GetBytes(ps);
string urlPath = "";
if (method == HttpRequestType.GET)
{
if (target.IndexOf("randCode.jsp?che=") < 0)
{
urlPath = string.Format("{0}?{1}", target, ps);
}
else
{
urlPath = target;
}
}
else
{
urlPath = target;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlPath);
HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = policy;
request.Timeout = timeOut * 0x3e8;
request.KeepAlive = keepAlive;
request.Method = method.ToString().ToUpper();
bool isPost = false;
isPost = request.Method.ToUpper() == "POST";
if (isPost)
{
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
}
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
request.Referer = refere;
request.Headers.Add("cookie", cookie);
request.Headers.Add("Cache-Control", "no-cache");
request.Accept = "*/*";
request.Credentials = CredentialCache.DefaultCredentials;
if (isPost)
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
response = (HttpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
byte[] buffer = StreamToBytes(responseStream);
returnStream = new MemoryStream(buffer);
string outCookie = response.Headers.Get("Set-Cookie");
if (outCookie != null)
{
outCookie = outCookie.Replace(",JSESSIONID=", "; JSESSIONID=");
cookie = SetCookies(cookie, outCookie);
}
}
catch (Exception)
{
returnStream = null;
}
finally
{
if (response != null)
response.Close();
if (responseStream != null)
responseStream.Close();
}
return returnStream;
}
private static byte[] StreamToBytes(Stream stream)
{
List<byte> bytes = new List<byte>();
int temp = stream.ReadByte();
while (temp != -1)
{
bytes.Add((byte)temp);
temp = stream.ReadByte();
}
return bytes.ToArray();
}
public static string SetCookies(string cookies, string setCookies)
{
Dictionary<string, string> newCookies = new Dictionary<string, string>();
if (cookies != null)
{
string[] tmpCookies = cookies.Split(";".ToCharArray());
for (int i = 0; i < tmpCookies.Length; i++)
{
string[] cookie = tmpCookies[i].Split('=');
if (cookie.Length != 2)
continue;
newCookies.Add(cookie[0].Trim(), cookie[1]);
}
}
if (setCookies != null)
{
string[] tmpCookies = setCookies.Split(";".ToCharArray());
for (int i = 0; i < tmpCookies.Length; i++)
{
string[] cookie = tmpCookies[i].Split('=');
if (cookie.Length != 2)
continue;
if (newCookies.ContainsKey(cookie[0].Trim()) == false)
newCookies.Add(cookie[0].Trim(), cookie[1]);
else
newCookies[cookie[0].Trim()] = cookie[1];
}
}
string szNewCookies = "";
Dictionary<string, string>.Enumerator it = newCookies.GetEnumerator();
while (it.MoveNext())
{
szNewCookies = szNewCookies + " " + it.Current.Key + "=" + it.Current.Value + ";";
}
if (szNewCookies.Length != 0)
szNewCookies = szNewCookies.Substring(1, szNewCookies.Length - 1);
return szNewCookies;
}
/// <summary>
/// 獲取一個HTML源
/// </summary>
/// <param name="target">目標地址</param>
/// <param name="refere">請求源</param>
/// <param name="cookie">Cookie</param>
/// <param name="parameters">參數(shù)列表</param>
/// <param name="encoding">編碼</param>
/// <param name="timeOut">超時時間</param>
/// <param name="keepAlive">是否建立持續(xù)連接</param>
/// <param name="method">請求類型</param>
/// <returns></returns>
public static string GetHtml(string target, string refere, ref string cookie, HttpRequestType method, Encoding encoding, int timeOut, bool keepAlive, params string[] parameters)
{
string returnHtml = string.Empty;
Stream stream = null;
try
{
stream = GetStream(target, refere, ref cookie, method, encoding, timeOut, keepAlive, parameters);
returnHtml = new StreamReader(stream, encoding).ReadToEnd();
}
catch (Exception)
{
returnHtml = string.Empty;
}
finally
{
if (stream != null)
{
stream.Close();
}
}
return returnHtml;
}
/// <summary>
/// 獲取一張圖片
/// </summary>
/// <param name="target">目標地址</param>
/// <param name="refere">請求源</param>
/// <param name="cookie">Cookie</param>
/// <param name="method">請求類型</param>
/// <returns></returns>
public static Bitmap GetImage(string target, string refere, ref string cookie, HttpRequestType method)
{
return GetImage(target, refere, ref cookie, method, Encoding.GetEncoding("gb2312"), 60, true);
}
/// <summary>
/// 獲取一張圖片
/// </summary>
/// <param name="target">目標地址</param>
/// <param name="refere">請求源</param>
/// <param name="cookie">Cookie</param>
/// <param name="method">請求類型</param>
/// <param name="encoding">編碼</param>
/// <param name="timeOut">超時時間</param>
/// <param name="keepAlive">是否建立持續(xù)連接</param>
/// <returns></returns>
public static Bitmap GetImage(string target, string refere, ref string cookie, HttpRequestType method, Encoding encoding, int timeOut, bool keepAlive, params string[] parameters)
{
Bitmap returnMap = null;
Stream stream = null;
try
{
stream = GetStream(target, refere, ref cookie, method, encoding, timeOut, keepAlive, parameters);
byte[] buf = new byte[stream.Length];
stream.Read(buf, 0, (int)stream.Length);
returnMap = new Bitmap(Image.FromStream(stream));
}
catch (Exception)
{
returnMap = null;
}
finally
{
if (stream != null)
{
stream.Close();
}
}
return returnMap;
}
public static HttpStatusCode GetResponseStatusCode(string strURL, string strCookieHeader, string strReferer, out string responseUrl, out string page)
{
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.ContentType = "text/html";
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Referer = strReferer;
myHttpWebRequest.Headers.Add("cookie:" + strCookieHeader);
HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
responseUrl = response.ResponseUri.OriginalString;
StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
page = sr.ReadToEnd();
return response.StatusCode;
}
catch (Exception ex)
{
responseUrl = "";
page = "";
return HttpStatusCode.RequestTimeout;
}
}
public static string GetPostXML(string strURL, string strArgs, string strCookieHeader, string strReferer)
{
WebClient myWebClient = new WebClient();
string postData = null;
byte[] byteArray;
byte[] responseArray;
WebHeaderCollection myWebHeaderCollection;
try
{
byteArray = Encoding.UTF8.GetBytes(strArgs);
postData = strArgs;
myWebClient.Headers.Add("Content-Type", "text/xml");
myWebClient.Headers.Add("Referer", strReferer);
myWebClient.Headers.Add("Accept-Language", "zh-cn");
myWebClient.Headers.Add("ContentLength", byteArray.Length.ToString());
myWebClient.Headers.Add(HttpRequestHeader.Cookie, strCookieHeader);
myWebHeaderCollection = myWebClient.Headers;
responseArray = myWebClient.UploadData(strURL, "POST", byteArray);
return Encoding.Default.GetString(responseArray);
}
catch (Exception ex)
{
return "";
}
}
#region 輔助方法
private static string patternRegion = "<input[^>]*?//b[^>]*?//bname=['/"]?{0}['/"]?//b[^>]*?value=['/"]?(?<value>[^/"]*)['/"]?//b[^>]*?>";
//返點
private static string REGBACKPOINT = "<span id=/"span_fee_0_0/">//d+(http://www.cnblogs.com/NoRoad/admin/file://d+%7c(///d%7B1,2%7D))//(((//d+(//d+%7C(///d%7B1,2%7D))))//)";
public static string UpdateAmountSQL = "UPDATE orders SET PayAmount = '{0}' WHERE bigcode = '{1}'";
private static readonly RegexOptions regexOptions = RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled;
public static string FindValueByName(string strHTML, string elementName)
{
string strValue = "";
Regex regexRegion = new Regex(string.Format(patternRegion, elementName), regexOptions); //解析Region的正則
try
{
MatchCollection mc = regexRegion.Matches(strHTML); //strHTML為要分析的網(wǎng)頁代碼
foreach (Match m in mc)
{
strValue = m.Groups["value"].Value;//取匹配到的命名組的值
}
}
catch (Exception ex)
{
}
return strValue;
}
public static string ChangeReturnHtml(string strHtml)
{
int begin = 0;
int end = 0;
while (begin != -1)
{
begin = strHtml.ToUpper().IndexOf("<script".ToUpper());
end = strHtml.ToUpper().IndexOf("</script>".ToUpper());
if (begin != -1)
strHtml = strHtml.Remove(begin, end - begin + 9);
}
strHtml = MoveOnload(strHtml);
return strHtml;
}
/// <summary>
/// 移除返回HTML中的onload事件
/// </summary>
/// <param name="InputString">返回的HTML</param>
/// <returns>處理后的html</returns>
private static string MoveOnload(string InputString)
{
int begin = 0;
int end = 0;
string _input = "";
while (begin != -1)
{
if (InputString.Contains("onload"))
{
_input = InputString.Substring(InputString.IndexOf("onload"));
begin = _input.IndexOf("onload");
end = _input.IndexOf("/">");
if (begin != -1 && end != -1)
{
_input = _input.Substring(begin, end - begin + 1);
InputString = InputString.Replace(_input, "");
}
}
else
begin = -1;
}
return InputString;
}
public static string GetUrlCookies(string Url, string cookieName)
{
Uri uri = new Uri(Url);
CookieContainer cc = CookieManger.GetUriCookieContainer(uri);
return CookieManger.GetCookiesString(cc, uri);
}
public static string GetSessionID(string cookies)
{
//Match match = Regex.Match(cookies, @"(?<SessionID>JSESSIONID=([^;]*))", RegexOptions.IgnoreCase);
try
{
Match match = Regex.Match(cookies, @"(?<SessionID>[^;,/s]*SESSIONID=[^;]*)", RegexOptions.IgnoreCase);
if (match.Success == true)
return match.Groups["SessionID"].Value.Trim();
else
return "";
}
catch (Exception ex)
{
return "";
}
}
public static void SetUrlCookies(string Url, string cookies)
{
if (cookies != null)
{
string[] tmpCookies = cookies.Split(";".ToCharArray());
//StringBuilder sbCookieName = new StringBuilder();
//StringBuilder sbCookieValue = new StringBuilder();
Uri uri = new Uri(Url);
for (int i = 0; i < tmpCookies.Length; i++)
{
string[] cookie = tmpCookies[i].Split('=');
if (cookie.Length != 2)
continue;
//sbCookieName.Append(cookie[0]);
//sbCookieValue.Append(cookie[1]);
InternetSetCookie(Url, cookie[0], cookie[1]/*+";expires=Sun,22-Feb-2099 00:00:00 GMT;"*/);
//InternetSetCookie(Url, "","");
}
//InternetSetCookie(Url, sbCookieName, sbCookieValue);
}
}
public static string FindBackpoint(string strHTML)
{
Regex regs = new Regex(REGBACKPOINT);
return regs.Match(strHTML).Groups[3].Value;
}
#endregion
}
}