Use httpRequest to log in to a website, then get the program example of the website information (transfer)

xiaoxiao2021-03-06  54

Original address: http://www.430000.net/printpage.asp? ArticleId = 69

Question: The content of some websites must be viewed after logging in, and its login information is saved in the session variable. In this way, components such as using Asphttp are difficult to properly get the information you want. SOLUTION: Implemented using HttpRequest and HttpResponse in ASP.NET. Important: 1. By attaching a cookieContainer to the HTTPRequest object, you can get the cookie of the representative session ID returned after logging. See the login method 2. This cookie is included in a cookieContainer and attached to another HttpRequest request, you can implement the SESSION restore. See the GetPage method source program as follows: getHttpinfo.aspx: <% @ page language = c # codebehind = gethttpinfo.aspx.cs autoeventwireup = false inherits = pdftest.getttpinfo%>

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 pdftest

{

///

/// summary description for Webform1.////

Public class gethtpinfo: system.web.ui.page

{

Protected static string cookieheader;

Private Void Page_Load (Object Sender, System.EventArgs E)

{

// put user code to initialize the page

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 (STRESULT);

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

{

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 receivingStream = Res. GetResponseSstream (); 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.

}

}

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)

{

INT i = 0, J;

While (i

{

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 ();

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.

}

}

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.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Required Method for Designer Support - Do Not Modified /// The Contents of this Method with the code editor./////

Private vidinitiRizeComponent ()

{

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

}

}

转载请注明原文地址:https://www.9cbs.com/read-116508.html

New Post(0)