Some website related content must be viewed after logon, And the login information is stored in the session variable. In this way, it is difficult to obtain the desired information by using components such as asphttp.

Source: Internet
Author: User

Some website related content must be viewed after logon, And the login information is stored in the session variable. In this way, it is difficult to obtain the desired information by using components such as asphttp.

A: Use httprequest and httpresponse in Asp.net.

Key points:
1. You can append a cookiecontainer to the httprequest object to obtain the cookie that represents the session ID returned after logon. See the login Method
2. If you include this cookie in one cookiecontainer and append it to another httprequest request, you can restore the session. See the getpage method.

SourceProgramAs follows:

gethttpinfo. aspx:
<% @ page Language = "C #" codebehind = "gethttpinfo. aspx. CS "autoeventwireup =" false "inherits =" release test. gethttpinfo "%>



webform1 </ title>









Gethttpinfo. aspx. CS:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
// Using system. Data. oledb;
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. net;
Using system. IO;
Using system. text;
Using system. Text. regularexpressions;
Using Microsoft. Data. ODBC;

Namespace verification test
{
/// <Summary>
/// Summary description for webform1.
/// </Summary>
Public class gethttpinfo: system. Web. UI. Page
{
Protected static string cookieheader;

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

String strresult;

If (httpcontext. Current. application ["cookieheader"]! = NULL)
{
Cookieheader = (string) httpcontext. Current. application ["cookieheader"];
}
Else
{
// Login into the website and keep the cookie for the session in the application variable
String strlogin = login ("http://www.thesiteyouwanttovisit/theloginpage.asp", "Action = & userid = & Password = ");
}

Strresult = getpage ("http://www.thesiteyouwanttovisit/theloginpage.asp", "Action = & Data = ");

// Write the result to HTM file
Filestream htmfile = new filestream ("C: \ save.htm", filemode. openorcreate );
Streamwriter Sw = new streamwriter (htmfile );
Sw. Write (strresult );
Sw. Close ();
Htmfile. Close ();

// Output the result
Response. Write (strresult );
}

Public static string login (string URL, string paramlist)
{
Httpwebresponse res = NULL;
String strresult = "";

Try
{

Httpwebrequest Req = (httpwebrequest) webrequest. Create (URL );
Req. method = "Post ";
Req. contenttype = "application/X-WWW-form-urlencoded ";
Req. allowautoredirect = false;
Cookiecontainer cookiecon = new cookiecontainer ();
Req. cookiecontainer = cookiecon;

Stringbuilder urlencoded = new stringbuilder ();
Char [] reserved = {'? ',' = ','&'};
Byte [] somebytes = NULL;

If (paramlist! = NULL)
{
Int I = 0, J;
While (I <paramlist. length)
{
J = paramlist. indexofany (reserved, I );
If (j =-1)
{
Urlencoded. append (httputility. urlencode (paramlist. substring (I, paramlist. Length-I )));
Break;
}
Urlencoded. append (httputility. urlencode (paramlist. substring (I, j-I )));
Urlencoded. append (paramlist. substring (J, 1 ));
I = J + 1;
}
Somebytes = encoding. utf8.getbytes (urlencoded. tostring ());
Req. contentlength = somebytes. length;
Stream newstream = Req. getrequeststream ();
Newstream. Write (somebytes, 0, somebytes. Length );
Newstream. Close ();
}
Else
{
Req. contentlength = 0;
}

Res = (httpwebresponse) Req. getresponse ();
Cookieheader = Req. cookiecontainer. getcookieheader (New uri (URL ));
Httpcontext. Current. application. Lock ();
Httpcontext. Current. application ["cookieheader"] = cookieheader;
Httpcontext. Current. application. Unlock ();

Stream receivestream = res. getresponsestream ();
Encoding encode = system. Text. encoding. getencoding ("UTF-8 ");
Streamreader sr = new streamreader (receivestream, encode );
Char [] READ = new char [256];
Int COUNT = Sr. Read (read, 0,256 );
While (count> 0)
{
String STR = new string (read, 0, count );
Strresult + = STR;
Count = Sr. Read (read, 0,256 );
}
}
Catch (exception E)
{
Strresult = E. tostring ();
}
Finally
{
If (res! = NULL)
{
Res. Close ();
}
}

Return strresult;
}

Public static string getpage (string URL, string paramlist)
{
Httpwebresponse res = NULL;
String strresult = "";

Try
{

httpwebrequest Req = (httpwebrequest) webrequest. create (URL);
req. method = "Post";
req. keepalive = true;
req. contenttype = "application/X-WWW-form-urlencoded";
cookiecontainer cookiecon = new cookiecontainer ();
req. cookiecontainer = cookiecon;
req. cookiecontainer. setcookies (New uri (URL), cookieheader);
stringbuilder urlencoded = new stringbuilder ();
char [] reserved = {'? ',' = ',' & '};
byte [] somebytes = NULL;

If (paramlist! = NULL)
{< br> int I = 0, J;
while (I {< br> J = paramlist. indexofany (reserved, I);
If (j =-1)
{< br> urlencoded. append (httputility. urlencode (paramlist. substring (I, paramlist. length-I);
break;
}< br> urlencoded. append (httputility. urlencode (paramlist. substring (I, j-I);
urlencoded. append (paramlist. substring (J, 1);
I = J + 1;
}< br> somebytes = encoding. utf8.getbytes (urlencoded. tostring ();
req. contentlength = somebytes. length;
stream newstream = req. getrequeststream ();
newstream. write (somebytes, 0, somebytes. length);
newstream. close ();
}< br> else
{< br> req. contentlength = 0;
}

Res = (httpwebresponse) Req. getresponse ();
Stream receivestream = res. getresponsestream ();
Encoding encode = system. Text. encoding. getencoding ("UTF-8 ");
Streamreader sr = new streamreader (receivestream, encode );
Char [] READ = new char [256];
Int COUNT = Sr. Read (read, 0,256 );
While (count> 0)
{
String STR = new string (read, 0, count );
Strresult + = STR;
Count = Sr. Read (read, 0,256 );
}
}
Catch (exception E)
{
Strresult = E. tostring ();
}
Finally
{
If (res! = NULL)
{
Res. Close ();
}
}

Return strresult;
}

# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

 

}
}

 

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.