Sample of small data collection written in ASP. net c #

Source: Internet
Author: User
Front-end page:
<% @ Page Language = "C #" codebehind = "gethttpcode. aspx. cs" autoeventwireup = "false" inherits = "coll_net.getpagehtml" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> collection example </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<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://www.0579.info/
</ASP: textbox>
<Br>
<Asp: button id = "webclientbutton" runat = "server" text = "Get With WebClient"> </ASP: button>
<Asp: button id = "gettext" style = "Z-INDEX: 101; left: 208px; position: absolute; top: 72px" runat = "server"
TEXT = "gettext"> </ASP: button>
<Br>
<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>

 

 

Background source code:
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;
Using system. Text. regularexpressions;
Namespace coll_net
{
/// <Summary>
/// Abstract description of getpagehtml.
/// </Summary>
Public class getpagehtml: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox urltext;
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. Button gettext;
Protected system. Web. UI. webcontrols. Button button1;
Private string pageurl = "";

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}

Private void webclientbutton_click (Object sender, system. eventargs E)
{
Pageurl = urltext. text;
WebClient WC = new WebClient ();
WC. Credentials = credentialcache. defaultcredentials;
/// 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 ();
}
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 Spaces
Contenthtml. Text = RegEx. Replace (contenthtml. Text, "\ s + ","");
}

# Code generated by region web Form Designer
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
/// 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
}
}

 

The following is a reference clip:

This is for accessing the Internet using the ISA Server proxy.
Modify the webrequest method:
Pageurl = urltext. text;
Webrequest request = webrequest. Create (pageurl );
WebProxy myproxy = new WebProxy ();
Myproxy = (WebProxy) request. proxy;
Myproxy. Address = new uri ("http: // proxy server: Port ");
Myproxy. Credentials = new networkcredential ("username", "password", "Domain Name ");
Request. Proxy = myproxy;
Webresponse response = request. getresponse ();


The following is a reference clip:
Another instance [〕
Private void page_load (Object sender, system. eventargs E)
{
String url = "/blog/upload/20034726115210282.gif ";
String stringfilename = URL. substring (URL. lastindexof ("/") + 1 );
String stringfilepath = request. physicalapplicationpath;
If (! Stringfilepath. endswith ("/") stringfilepath + = "/";
Msxml2.xmlhttp _ XMLHTTP = new msxml2.xmlhttpclass ();
_ XMLHTTP. Open ("get", URL, false, null, null );
_ XMLHTTP. Send ("");
If (_ XMLHTTP. readystate = 4)
{
If (system. Io. file. exists (stringfilepath + stringfilename ))
System. Io. file. Delete (stringfilepath + stringfilename );
System. Io. filestream FS = new system. Io. filestream (stringfilepath + stringfilename, system. Io. filemode. createnew );
System. Io. binarywriter W = new system. Io. binarywriter (FS );
W. Write (byte []) _ XMLHTTP. responsebody );
W. Close ();
FS. Close ();
Response. Write ("the file has been obtained. <Br> <a href = '"+ request. applicationpath + stringfilename +" 'target =' _ blank '> ");
Response. Write ("View" + stringfilename + "</a> ");
}
Else
Response. Write (_ XMLHTTP. statustext );
Response. End ();
}

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.