您的位置:首页 > 产品设计 > UI/UE

Get data from specified URI using WebRequest and WebResponse(读取网页数据并存入对应html文档)

2009-10-30 18:18 731 查看
  WebRequest is a request to send messages to a URI to send messages, URI as a parameter is passed to Create () method. And we take WebResponse class as data obtained from the server. The method WebRequest.GetResponse () will indeed send the request to the Web server, create a Response object, and then check the returned data. Like the WebClient object, you can get a stream of data, however, the data flow is obtained by WebResponse.GetResponseStream () method.

  Here is a smple to show you how to get data from URI and save as a html file:

public static void GetDataFromUri(string uri, string htmlFile)
{
//Makes a request to specified URI.
System.Net.WebRequest request = WebRequest.Create(uri);
System.Net.WebResponse webResponse = request.GetResponse();

//Initialize a new instance of the System.IO.StreamReader class for the web response stream,
//with the specified character encoding, and get all data.
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.Default);
string dataBuffer = sr.ReadToEnd();

FileStream output = new FileStream(htmlFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] recvBuffer = System.Text.Encoding.Default.GetBytes(dataBuffer);
BinaryWriter bw = new BinaryWriter(output);
bw.Write(recvBuffer);

//If we use this method to write data, some characters will be garbled.
//StreamWriter sw = new StreamWriter(output);
//sw.Write(dataBuffer);
}
  

  Note this: If we use StreamWriter to write directly, we will get some garbled characters in the html file. Of course that's the encoding problem.

Go to my home page for more posts
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐