Asp.net requires three types of webpage content capturing: WebRequest, WebResponse, and StreamReader.
The namespace of WebRequest and WebResponse is:
System. Net
The namespace of StreamReader is:
System. IO
ASP. NET captures the core code of webpage content
- WebRequest request = WebRequest.Create("http://www.cftea.com/");
- WebResponse response = request.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
The Create method of the WebRequest class is a static method, and the parameter is the URL of the webpage to be crawled;
Encoding specifies the Encoding. Encoding has global common Encoding such as ASCII, UTF32, and UTF8, but does not have the gb2312 Encoding attribute. Therefore, we use GetEncoding to obtain gb2312 Encoding.
Example
- <% @ Page Language ="C #"%>
- <% @ Import Namespace ="System. Net"%>
- <% @ Import Namespace ="System. IO"%>
- <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Script runat ="Server">
- VoidPage_Load (ObjectSender, EventArgs e)
- {
- Try
- {
- WebRequest request = WebRequest. Create ("Http://www.cftea.com /");
- WebResponse response = request. GetResponse ();
- StreamReader reader =NewStreamReader (response. GetResponseStream (), Encoding. GetEncoding ("Gb2312"));
- Tb. Text = reader. ReadToEnd ();
- Reader. Close ();
- Reader. Dispose ();
- Response. Close ();
- }
- Catch(Exception ex)
- {
- Tb. Text = ex. Message;
- }
- }
- </Script> Http://www.w3.org/1999/xhtml">
- <Head runat ="Server">
- <Title> capture webpage content-Qianyi Network </title>
- </Head>
- <Body>
- <Form id ="Form1"Runat ="Server">
- <Div>
- <Asp: TextBox ID ="Tb"Runat ="Server"Width ="500"Height ="300"TextMode ="MultiLine"> </Asp: TextBox>
- </Div>
- </Form>
- </Body>
- </Html>
-
In this way, ASP. NET captures webpage content.
- Differences between Bind and Eval in ASP. NET: two types of binding
- Simple common ASP. NET Code 2)
- Simple common ASP. NET code 1)
- ASP. NET tips: Use of unmanaged COM components
- Another implementation of concatenating strings in ASP. NET: Response. Write