How to obtain the HTML source code again

Source: Internet
Author: User
Code
Geturlhtml. aspx

<% @ Page Language = " C # " Codebehind = " Geturlhtml. aspx. CS " SRC = " Geturlhtml. aspx. CS " Autoeventwireup = " False " Inherits = " Lion. Web. Forum. webform1 " %>
< Html >
< Head >
< Title > Lion interactive network => Use WebClient and webrequest to obtain webpagesSource code </ Title >
< Meta HTTP - Equiv = " Content-Type " Content = " Text/html; charset = gb2312 " >
</ Head >
< Body >
< Form runat = " Server " >
< ASP: textbox ID = " Urltext " Runat = " Server " Width = " 50% " > HTTP: // Www.lionsky.net/mywebsite/index.aspx </ASP: textbox>
< ASP: button ID = " Button1 " Runat = " Server " Text = " Use WebClient to obtain " > </ ASP: button > & Nbsp;
< ASP: button ID = " Button2 " Runat = " Server " Text = " Use webrequest to obtain " > </ ASP: button > < BR >
< ASP: textbox ID = " Contenthtml " Runat = " Server " Width = " 100% " Height = " 360px " Textmode = " Multiline " > </ ASP: textbox >
</ Form >
</ Body >
</ Html >

Geturlhtml. aspx. CS

Using System;
Using System. collections;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. Web;
Using System. Web. sessionstate;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. htmlcontrols;

Namespace Lion. Web. Forum
{
/// <Summary>
/// Abstract description of webform1.
/// </Summary>
Public Class Webform1: system. Web. UI. Page
{
Protected System. Web. UI. webcontrols. textbox textbox1;
Protected System. Web. UI. webcontrols. Button button1;
Protected System. Web. UI. webcontrols. Button button2;
Protected System. Web. UI. webcontrols. textbox urltext;
Protected System. Web. UI. webcontrols. textbox contenthtml;
Protected System. Web. UI. webcontrols. textbox textbox2;

# Region Generated by the web Form DesignerCode
Override Protected Void Oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base . Oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// The content of this method.
/// </Summary>
Private Void Initializecomponent ()
{
This . Button1.click + = New System. eventhandler ( This . Button#click );
This . Button2.click + = New System. eventhandler ( This . Button2_click );

}
# Endregion

Private Void Button#click ( Object Sender, system. eventargs E)
{
String Pageurl = Urltext. text;
System. net. WebClient WC = New System. net. WebClient ();
WC. Credentials = System. net. credentialcache. defaultcredentials;

/// Method 1:
Byte [] pagedata = WC. downloaddata (pageurl );
Contenthtml. Text = System. Text. encoding. Default. getstring (pagedata );

/// Method 2:
/// * ************** Code start **********
/// Stream resstream = WC. openread (pageurl );
/// Streamreader sr = new streamreader (resstream, system. Text. encoding. Default );
/// Contenthtml. Text = Sr. readtoend ();
/// Resstream. Close ();
/// * ************* Code end ********
///
WC. Dispose ();

}

Private Void Button2_click ( Object Sender, system. eventargs E)
{
String Pageurl = Urltext. text;
System. net. webrequest request = System. net. webrequest. Create (pageurl );
System. net. webresponse response = Request. getresponse ();
System. Io. Stream resstream = Response. getresponsestream ();
System. Io. streamreader SR = New System. Io. streamreader (resstream, system. Text. encoding. Default );
Contenthtml. Text = Sr. readtoend ();
Resstream. Close ();
Sr. Close ();

}
}
}

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.