Download the source code and files of the webpage from the website to which Asp.net needs to log on.

Source: Internet
Author: User

This is the file download class: CopyCode The Code is as follows: using system;
Using system. IO;
Using system. net;
Using system. Web;

Public class srwebclient
{
Cookiecontainer cookie;
Public srwebclient ()
{
Cookie = new cookiecontainer ();
}

/// <Tgdata>
/// <Alias> download the WebSource code</Alias>
/// </Tgdata>
Public String downloadhtml (string URL)
{
Httpwebrequest request = httpwebrequest. Create (URL) as httpwebrequest;
Request. cookiecontainer = cookie;
Request. allowautoredirect = false;

Webresponse res = request. getresponse ();
String r = "";

Streamreader S1 = new streamreader (res. getresponsestream (), system. Text. encoding. Default );
Try
{
R = s1.readtoend ();
}
Catch (exception ER)
{
Log L = new log ();
L. writelog ("Download web error", er. tostring ());
}
Finally
{
Res. Close ();
S1.close ();
}

Return R;
}

/// <Tgdata>
/// <Alias> download an object </alias>
/// </Tgdata>
Public long downloadfile (string fileurl, string filesavepath)
{
Long filelength = 0;
Httpwebrequest Req = httpwebrequest. Create (fileurl) as httpwebrequest;

Req. cookiecontainer = cookie;
Req. allowautoredirect = true;

Httpwebresponse res = Req. getresponse () as httpwebresponse;
System. Io. Stream stream = res. getresponsestream ();
Try
{
Filelength = res. contentlength;

Byte [] B = new byte [512];

Int nreadsize = 0;
Nreadsize = stream. Read (B, 0,512 );

System. Io. filestream FS = system. Io. file. Create (filesavepath );
Try
{
While (nreadsize> 0)
{
FS. Write (B, 0, nreadsize );
Nreadsize = stream. Read (B, 0,512 );
}
}
Finally
{
FS. Close ();
}
}
Catch (exception ER)
{
Log L = new log ();
L. writelog ("File Download error", er. tostring ());
}
Finally
{
Res. Close ();
Stream. Close ();
}

Return filelength;
}

/// <Tgdata>
/// <Alias> submit data </alias>
/// </Tgdata>
Public void request (string requestpageurl, requestdata data)
{
String strurl = requestpageurl;
Httpwebrequest request = httpwebrequest. Create (strurl) as httpwebrequest;
Httpwebresponse response;

String postdata = data. getdata ();
Request. Referer = requestpageurl;
Request. allowautoredirect = false;
Request. useragent = "Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 5.0 )";
Request. cookiecontainer = cookie;

Uri u = new uri (strurl );

If (postdata. length> 0) // the POST method is used to contain the data to be submitted.
{
// As a form request
Request. contenttype = "application/X-WWW-form-urlencoded ";
// The method is post.
Request. method = "Post ";

// Replace the submitted data with a byte array
Byte [] B = system. Text. encoding. Default. getbytes (postdata );
Request. contentlength = B. length;

Stream Sw = request. getrequeststream (); // start to submit data
Sw. Write (B, 0, B. Length );
Sw. Close ();
}

Response = request. getresponse () as httpwebresponse;
Response. Close ();
}
}

This is the submitted data class:Copy codeThe Code is as follows: using system. collections;
Using system. IO;

Public class requestdata
{
Arraylist arr = new arraylist ();
Public requestdata ()
{

}

Public String getdata ()
{
String r = "";

For (INT I = 0; I <arr. Count; I ++)
{
Data d = (data) Arr [I];
If (R. length> 0)
R + = "&";
R + = D. Field + "=" + D. value;
}
Return R;
}

Public void addfield (string field, string value)
{
Data A = new data ();
A. Field = field;
A. value = value;

Arr. Add ();
}

Struct data
{
Public String Field, value;
}
}

After the code is pasted, the following is the test code, which is hidden because some data involves the customer's information.Copy codeThe Code is as follows: Using nunit. Framework;

[Testfixture]
Public class testweb
{
[Test]
Public void testdownseorder ()
{
Requestdata DATA = new requestdata ();
Data. addfield ("name", "ABC ");
Data. addfield ("password", "121 ");

Srwebclient web = new srwebclient ();
Web. Request ("http: // 127.0.0.1/login. asp", data );

String S = web. downloadhtml ("http: // 127.0.0.1/dingdan. asp ");
System. Console. writeline (s );
}

[Test]
Public void testdownfile ()
{
Requestdata DATA = new requestdata ();
Data. addfield ("name", "AAA ");
Data. addfield ("password", "BBB ");

Srwebclient web = new srwebclient ();
Web. Request ("http: // 127.0.0.1/login. asp", data );

Web. downloadfile ("http: // 127.0.0.1/download. asp? Fileid = 1 ", @" C: \ a.txt ");
}
}

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.