|
发表于 2013-7-17 15:12:56
|
显示全部楼层
public static bool WebGetContext(string strUrl, ref CookieCollection cookiesCollection, ref string strContext, bool isAllowAutoRedirect, string strReferer)
{
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);
req.Method = "GET";
req.Headers.Add("Accept-Encoding", "gzip, deflate");
req.UserAgent = _strHeadUserAgent;
req.Accept = _strHeadAccept;
req.AllowAutoRedirect = isAllowAutoRedirect;
req.ReadWriteTimeout = nTimeOut;
if (!string.IsNullOrEmpty(strReferer))
req.Referer = strReferer;
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(cookiesCollection);
using(HttpWebResponse wr = (HttpWebResponse)req.GetResponse())
{
//重定向获取cookies
cookiesCollection = req.CookieContainer.GetCookies(req.RequestUri);
if (CoverEncod(wr, ref strContext))
return true;
}
return false;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public static bool CoverEncod(HttpWebResponse wr, ref string strContext)
{
try
{
//获取编码格式
string encoding = "utf-8";
Regex regex = new Regex(@"(?<=charset=).*");
Match match = regex.Match(wr.ContentType);
if (match.Success)
encoding = match.Value.ToString();
//获取正文内容
if (wr.Headers.ToString().Contains("gzip"))
{
using (GZipStream gzipStream = new GZipStream(wr.GetResponseStream(), CompressionMode.Decompress))
{
using (StreamReader streamReader = new StreamReader(gzipStream, Encoding.GetEncoding(encoding)))
strContext = streamReader.ReadToEnd();
}
}
else
{
using (StreamReader streamReader = new StreamReader(wr.GetResponseStream(), Encoding.GetEncoding(encoding)))
strContext = streamReader.ReadToEnd();
}
return true;
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
return false;
}
} |
|