Use. NET Framework provides the WebClient class and the WebRequest class, we can easily get the source code of the given URL address, very simple, the following is a complete example of C #.

Source: Internet
Author: User
Tags gettext visual studio
. NET Framework |client|request|web| source code

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> get the page source code </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" > get any Web source code </div>
<asp:textbox id= "Urltext" runat= "Server" width= "400px" >http://dotnet.aspx.cc/content.aspx
</asp:TextBox>
<asp:button id= "Webclientbutton" runat= "server" text= "WebClient" ></asp:Button>
<asp:button id= "Webrequestbutton" runat= "Server" text= "WebRequest" ></asp:Button>
<br>
<asp:textbox id= "contenthtml" runat= "Server" width= "100%" height= "360px" textmode= "MultiLine" >
</asp:TextBox>
</form>
</body>
</HTML>

 getpagehtml.aspx.csusing 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;
using System.Text.RegularExpressions;
Namespace Emeng.exam
... {
/**////<summary>
///getpagehtml's summary description.
///</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;
protected System.Web.UI.WebControls.Button GetText;
private String pageurl = ";

private void Page_Load (object sender, System.EventArgs e)
... {}

Web Form Designer generated code#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
... {
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Designer supports required methods-do not use the Code editor to modify
The contents 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. Gettext.click + = new System.EventHandler (this. Gettext_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;

/**////Method One:
byte[] Pagedata = WC. Downloaddata (Pageurl);
Contenthtml.text = Encoding.Default.GetString (pagedata);


/**////Method II:
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 ();
&NBSP;SR. Close ();
}

private void Gettext_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 ();
Contenthtml.text = Regex.Replace (Contenthtml.text, "<[^>]*>", "");
Replace space
Contenthtml.text = Regex.Replace (Contenthtml.text, "\s+", "");
}
}
}



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.