webclient无法获取html文件,c# WebClient 网站页面抓取(获取)的三种方法

c#.net网站页面抓取(获取)的三种方法 :WebRequest /WebClient /webbrowser

using System.Net;

using System.IO;

using System.Text;

Response.Write(“方法一:


”);

//方法一:

// Create a request for the URL.

WebRequest request = WebRequest.Create(“http://www.hao123.com”);

// If required by the server, set the credentials.

request.Credentials = CredentialCache.DefaultCredentials;

// Get the response.

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Display the status.

Response.Write(response.StatusDescription);

// Get the stream containing content returned by the server.

Stream dataStream = response.GetResponseStream();

// Open the stream using a StreamReader for easy access.

StreamReader reader = new StreamReader(dataStream, Encoding.Default);

// Read the content.

string responseFromServer = reader.ReadToEnd();

// Display the content.

Response.Write(responseFromServer);

// Cleanup the streams and the response.

reader.Close();

dataStream.Close();

response.Close();

Response.Write(“方法二:


”);

//方法二:

// Add a user agent header in case the

// requested URI contains a query.

client.Headers.Add(“user-agent”, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)”);

Stream data = client.OpenRead(“http://www.hao123.com”);

StreamReader reader = new StreamReader(data, Encoding.Default);

string s = reader.ReadToEnd();

Response.Write(s);

data.Close();

reader.Close();

Response.Write(“方法三:


”);

//方法三:

//client.DownloadFile(“http://www.hao123.com”,”123.htm”);

string reply = client.DownloadString(“http://www.hao123.com”);

Response.Write(reply);

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# WebClient 网站页面抓取(获取)的三种方法!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值