ASP. NET

Source: Internet
Author: User

Testweb. aspx. CS Background File

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. net;
Using system. IO;
Using system. text;

Public partial class testweb: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

} // <Summary>
/// Generate a webpage File
/// </Summary>
/// <Param name = "url"> remote URL </param>
/// <Param name = "FILENAME"> Generate the file name path </param>
/// <Param name = "pagecode"> Target URL page encoding </param>
Protected void downurltofile (string URL, string filename, string pagecode)
{
Try
{
// Encoding
Encoding encode = encoding. getencoding (pagecode );
// Request URL
Httpwebrequest Req = (httpwebrequest) webrequest. Create (URL );
// Set timeout (10 seconds)
Req. Time out = 10000;
This. notfolderiscreate (filename );
// Obtain response
Httpwebresponse rep = (httpwebresponse) Req. getresponse ();
// Create streamreader and streamwriter file stream object
Streamreader sr = new streamreader (REP. getresponsestream (), encode );
Streamwriter Sw = new streamwriter (server. mappath (filename), false, encode );
// Write content
Sw. Write (Sr. readtoend ());
// Clear the current cache and write the cache into the file
Sw. Flush ();
// Release related object Resources
Sw. Close ();
Sw. Dispose ();
Sr. Close ();
Sr. Dispose ();
Response. Write ("generate file" + filename + "success ");
}
Catch (exception ex)
{
Response. Write ("generate file" + filename + "failed, cause:" + ex. Message );
}
}
/// <Summary>
/// Create if the folder does not exist
/// </Summary>
/// <Param name = "FILENAME"> file name path </param>
Protected void notfolderiscreate (string filename)
{
String fileatdir = server. mappath (path. getdirectoryname (filename ));
If (! Directory. exists (fileatdir ))
Directory. createdirectory (fileatdir );
}
Protected void button#click (Object sender, eventargs E)
{
String txtstr = This. textbox1.text. tostring ();
String rolname = system. guid. newguid (). tostring ();
This. downurltofile ("" + txtstr + "", "html/" + rolname + ". htm", "gb2312 ");

}
}

 

Testweb. aspx foreground File

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "testweb. aspx. cs" inherits = "testweb" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



No title page









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.