实用工具类 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; } } }
相关文章推荐
- 自定义volley框架StringRequest类 ——个方法发送各种请求(get post 参数 cookie)
- GET和POST测试(支持需要登录的接口调用:高级功能->填写cookie)
- PHP中使用curl及代理IP模拟post提交【两种实用方法】
- 弃用的异步get和post方法之代理方法
- Spider网络爬虫学习——通过Get、Post方法直接抓取网页内容
- PHP处理网页表单GET和POST方法
- jquery实现读取页面load get post ajax的几种代码方法
- PHP Module中获取$_GET/$_POST/$_COOKIE的方法研究
- python爬虫中get和post方法介绍以及cookie作用
- [置顶] 针对 android端模拟教务系统登陆,主要针对抓包过程,post,get请求,和解析网页和cookie(一)
- curl提交GET,POST,Cookie的简单方法
- Linux实用技巧-CURL实现HTTP的GET POST方法
- php使用curl 实现GET和POST请求(抓取网页,上传文件),支持跨项目和跨服务器
- WPF带cookie get/post请求网页,下载文件,图片,可保持会话状态
- HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理
- 【末世旅行之Java】HttpClient工具类,包含POST和GET请求,使用了连接池和代理
- Apache 大三方法HTTP请求链接响应之HttpClient的GET和POST工具类封装
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- WebService支持Post和Get方法