[ASP. NET] C # obtain webpage source code using WebClient and webrequest

Source: Internet
Author: User

Using the WebClient class and webrequest class provided by the. NET Framework, we can easily obtain the source code of the given URL address. The following is a complete example of C.

 

View examples

Getpagehtml. aspx

<% @ Page Language = "C #" validaterequest = "false" codebehind = "getpagehtml. aspx. cs"
Autoeventwireup = "false" inherits = "emeng. Exam. getpagehtml" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> obtain the source code of the webpage </title>
<Meta name = "generator" content = "Microsoft Visual Studio 7.0">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "aspnetbuffer" method = "Post" runat = "server">
<Div align = "center" style = "font-weight: bold"> obtain the source code of any webpage </div>
<Asp: textbox id = "urltext" runat = "server" width = "400px"> http://dotnet.aspx.cc/content.aspx
</ASP: textbox>
<Asp: button id = "webclientbutton" runat = "server" text = "Get With WebClient"> </ASP: button>
<Asp: button id = "webrequestbutton" runat = "server" text = "Get With webrequest"> </ASP: button>
<Br>
<Asp: textbox id = "contenthtml" runat = "server" width = "100%" Height = "360px" textmode = "multiline">
</ASP: textbox>
</Form>
</Body>
</Html>

Getpagehtml. 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;
Using system. IO;
Using system. net;
Using system. text;

Namespace emeng. Exam
{
/// <Summary>
/// Abstract description of getpagehtml.
/// </Summary>
Public class getpagehtml: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button webclientbutton;
Protected system. Web. UI. webcontrols. Button webrequestbutton;
Protected system. Web. UI. webcontrols. textbox contenthtml;
Protected system. Web. UI. webcontrols. textbox urltext;
Private string pageurl = "";

Private void page_load (Object sender, system. eventargs E)
{}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. webclientbutton. Click + = new system. eventhandler (this. webclientbutton_click );
This. webrequestbutton. Click + = new system. eventhandler (this. webrequestbutton_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion

Private void webclientbutton_click (Object sender, system. eventargs E)
{
Pageurl = urltext. text;
WebClient WC = new WebClient ();
WC. Credentials = credentialcache. defaultcredentials;

Strresult = WC. downloadstring (pageurl );
/// Method 1:
Byte [] pagedata = WC. downloaddata (pageurl );
Contenthtml. 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 webrequestbutton_click (Object sender, system. eventargs E)
{
Pageurl = urltext. text;
Webrequest request = webrequest. Create (pageurl );
Webresponse response = request. getresponse ();
Stream resstream = response. getresponsestream ();
Streamreader sr = new streamreader (resstream, system. Text. encoding. Default );
Contenthtml. Text = Sr. readtoend ();
Resstream. Close ();
Sr. Close ();
}
}
}

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.