现在的位置: 首页.NET技术 > 正文
C#用HttpWebRequest通过代理服务器验证后抓取网页内容
关键词:Proxy ┊ 来源: 原创收藏

string urlStr = "http://www.domain.com";                            //设定要获取的地址
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr);    //建立HttpWebRequest对象
hwr.Timeout = 60000;                                                //定义服务器超时时间
WebProxy proxy = new WebProxy();                                    //定义一个网关对象
proxy.Address = new Uri("http://proxy.domain.com:3128");            //网关服务器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233");    //用户名,密码
hwr.UseDefaultCredentials = true;                                    //启用网关认証
hwr.Proxy = proxy;                                                    //设置网关
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse();            //取得回应
Stream s = hwrs.GetResponseStream();                                //得到回应的流对象
StreamReader sr = new StreamReader(s, Encoding.UTF8);                //以UTF-8编码读取流
StringBuilder content = new StringBuilder();                       
while (sr.Peek() != -1)                                                //每次读取一行,直到
{                                                                    //下一个字节没有内容
    content.Append(sr.ReadLine()+"\r\n");                            //返回為止
}                                                                   
return content.ToString() ;                                            //返回得到的字符串
相关文章
    暂无相关文章
本文由 jack 发布于 1911天 21小时 46分钟前,目前已有 2970 人浏览
欢迎大家转载分享,请注明来源及链接;商业媒体转载请获得授权,谢谢合作!
 

添加评论