httphelp web自动化
public class HttpHelper
{
public static CookieContainer CookieContainers = new CookieContainer();
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <param name="method">"POST" or "GET"</param>
/// <param name="data">when the method is "POST", the data will send to web server, if the method is "GET", the data should be string.empty</param>
/// <returns></returns>
public static string GetPostResponse(string url, string data,string referer)
{
StreamReader stReader = null;
HttpWebRequest request=null;
HttpWebResponse response=null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
string dataStr = data;
byte[] postData = Encoding.GetEncoding("GBK").GetBytes(data);
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
request.Referer = referer;
request.CookieContainer = CookieContainers;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Headers.Add("Cache-Control", "max-age=0");
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
Stream st = request.GetRequestStream();//将st名为写入数据流
st.Write(postData, 0, postData.Length);//写入st数据流
st.Close();
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
st = response.GetResponseStream();
CookieContainers.Add(response.Cookies);
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress, true);
}
string htmlText;
stReader = new StreamReader(st, Encoding.GetEncoding("GBK"));
htmlText = stReader.ReadToEnd();
return htmlText;
}
catch (Exception ex)
{
throw ex;
}
finally
{
stReader.Close();
response.Close();
}
}
public static string GetResponse(string url, string referer)
{
HttpWebRequest request=null;
HttpWebResponse response=null;
StreamReader stReader =null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.ProtocolVersion = HttpVersion.Version11;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2";
request.ContentType = "application/x-www-form-urlencoded";
if (referer != "")
{
request.Referer = referer;
}
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.CookieContainer = CookieContainers;
request.Headers.Add("Cache-Control", "max-age=0");
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
Stream st = response.GetResponseStream();
CookieContainers.Add(response.Cookies);
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
st = new GZipStream(st, CompressionMode.Decompress, true);
}
string htmlText;
stReader = new StreamReader(st, Encoding.GetEncoding("GBK"));
htmlText = stReader.ReadToEnd();
return htmlText;
}
catch (Exception ex)
{
throw ex;
}
finally
{
stReader.Close();
response.Close();
}
}
}