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
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
相关文章推荐
- Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
- send a client certificate by using the HttpWebRequest and HttpWebResponse classes
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- Using WebRequest and WebResponse classes
- Get the webpage data from URI.
- Using Timer to get history values from EDNA database, an error is prompted in the page : "Sys.WebForms.PageRequestManagerTimeout
- JSP URI/URL - How to get the request URI, URL, and Context from a JSP
- Web网页中动态数据区域的识别与抽取 Dynamical Data Regions Identification and Extraction in Web Pages
- Node(6) Accessing GET and POST data using http.ServerRequest
- C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
- 使用R语言和XML包抓取网页数据-Scraping data from web pages in R with XML package
- Use C# to get JSON Data from the Web and Map it to .NET Class => Made Easy! 转
- .Net 下用 WebRequest、WebResponse组件 GET、POST 收发网页
- Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop
- 【转载】C#网页采集数据的几种方式(WebClient、WebBrowser和HttpWebRequest/HttpWebResponse)
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- 使用HttpWebRequest与HttpWebResponse抓取网页数据
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework