您的位置:首页 > 其它

实用工具类 PageLoader,以get和post方法读取网页, 支持代理和cookie

2011-04-15 14:50 549 查看
using System;
using System.Text;
using System.IO;
using System.Linq;
using System.Net;

/// <summary>
/// 实用工具类。这个类支持以get和post方法读取网页, 支持代理和cookie
/// </summary>
public class PageLoader
{
public string _proxy = null;
public string Proxy { get { return _proxy; } set { _proxy = value; } }

class TrustAllCertificatePolicy : ICertificatePolicy
{
public TrustAllCertificatePolicy()
{

//
// TODO: 在此处添加构造函数逻辑

}
public bool CheckValidationResult(ServicePoint sp, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
{
return true;// 这是最关键的,返回true,告诉客户端忽略证书名称不匹配!
}
}

public bool LoadPage(string url, string referer, string postdata, CookieContainer cookies, out MemoryStream outStream)
{

try
{

HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(new Uri(url));
Req.ServicePoint.Expect100Continue = false;
Req.ReadWriteTimeout = 60000;
if (!string.IsNullOrEmpty(referer))
Req.Referer = referer;
Req.CookieContainer = cookies;
//Req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
// Req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */*";
Req.Accept = "*/*";
Req.ContentType = "application/x-www-form-urlencoded";
Req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; SV1; .NET CLR 2.0.50727)";
//Req.Headers.Add("Accept-Encoding", "gzip,deflate");
Req.Headers.Add("Accept-Language", "zh-cn");
//Req.KeepAlive = true;

System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();

if (!string.IsNullOrEmpty(_proxy))
{
Req.Proxy = new WebProxy(_proxy);
}
if (!string.IsNullOrEmpty(postdata))
{

Req.Expect = "";
Req.ContentType = "application/x-www-form-urlencoded";
Req.Method = "POST";
byte[] buf = System.Text.Encoding.ASCII.GetBytes(postdata);
Req.ContentLength = buf.Length;
Stream reqStream = Req.GetRequestStream();
reqStream.Write(buf, 0, buf.Length);
reqStream.Flush();
reqStream.Close();
}

Req.Timeout = 60000;
HttpWebResponse Res = (HttpWebResponse)Req.GetResponse();

int Len = 0;
byte[] Buffer = new byte[1024];
MemoryStream bufStream = new MemoryStream();
Stream resStream = Res.GetResponseStream();
string str;
string str2;
str = Res.Headers.ToString();
resStream.ReadTimeout = 60000;
Len = resStream.Read(Buffer, 0, 1024);
while (Len > 0)
{
bufStream.Write(Buffer, 0, Len);
Len = resStream.Read(Buffer, 0, 1024);
}
resStream.Close();
bufStream.Position = 0;

outStream = bufStream;

return true;
}
catch { }
{
outStream = null;
return false;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: