Example of Web Page Content captured by ASP. NET

Source: Internet
Author: User

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

 
 
  1. WebRequest request = WebRequest.Create("http://www.cftea.com/");  
  2. WebResponse response = request.GetResponse();  
  3. 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

 
 
  1. <% @ Page Language ="C #"%>
  2. <% @ Import Namespace ="System. Net"%>
  3. <% @ Import Namespace ="System. IO"%>
  4. <! DOCTYPE html PUBLIC"-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Script runat ="Server">
  5. VoidPage_Load (ObjectSender, EventArgs e)
  6. {
  7. Try 
  8. {
  9. WebRequest request = WebRequest. Create ("Http://www.cftea.com /");
  10. WebResponse response = request. GetResponse ();
  11. StreamReader reader =NewStreamReader (response. GetResponseStream (), Encoding. GetEncoding ("Gb2312"));
  12. Tb. Text = reader. ReadToEnd ();
  13. Reader. Close ();
  14. Reader. Dispose ();
  15. Response. Close ();
  16. }
  17. Catch(Exception ex)
  18. {
  19. Tb. Text = ex. Message;
  20. }
  21. }
  22. </Script> Http://www.w3.org/1999/xhtml">
  23. <Head runat ="Server">
  24. <Title> capture webpage content-Qianyi Network </title>
  25. </Head>
  26. <Body>
  27. <Form id ="Form1"Runat ="Server">
  28. <Div>
  29. <Asp: TextBox ID ="Tb"Runat ="Server"Width ="500"Height ="300"TextMode ="MultiLine"> </Asp: TextBox>
  30. </Div>
  31. </Form>
  32. </Body>
  33. </Html>
  34.  

In this way, ASP. NET captures webpage content.

  1. Differences between Bind and Eval in ASP. NET: two types of binding
  2. Simple common ASP. NET Code 2)
  3. Simple common ASP. NET code 1)
  4. ASP. NET tips: Use of unmanaged COM components
  5. Another implementation of concatenating strings in ASP. NET: Response. Write

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.