After searching online for a long time, the following methods were created.
When the sessionid information is not found in the document. Cookie of a Web browser,
Then, send a request using webrequest and use response. headers ["Set-cookie"] to obtain the sessionid in the middle of this attribute.
1 Cookiecollection cookiecheckcode;
2 Cookiecontainer container = New Cookiecontainer ();
3
4 Httpwebresponse response = Null ;
5 Httpwebrequest request = Null ;
6
7 Request = (Httpwebrequest) webrequest. Create ( String . Format ( " Http: // {0}/login. aspx " , Host ));
8 Request. Referer = " Http :// " + Host;
9 Request. Method = " Get " ;
10 Request. Accept = " */* " ;
11 Request. Timeout = 5000 ;
12 Request. useragent = " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; Maxthon) " ;
13 Response = (Httpwebresponse) request. getresponse ();
14 Responsestream = Response. getresponsestream ();
15
16
17 Cookie cookie4 = New Cookie ( " ASP. net_sessionid " , This . Getsessionid (response. headers [ " Set-Cookie " ]);
18 Cookie4.domain = Host;
19 Cookiecheckcode. Add (cookie4 );
20
21 Responsestream. Close ();
22 Response. Close ();
23
24 Container. Add (cookiecheckcode );
Getsessionid method:
1 /**/ /// <Summary>
2 /// Obtain the sessionid from the response headers.
3 /// </Summary>
4 /// <Param name = "value"> [Response]. [headers]. ["Set-cookie"] </Param>
5 /// <Returns> </returns>
6 Private String Getsessionid ( String Value)
7 {
8 String Starttag = " ASP. net_sessionid = " ;
9 Int Indexend = Value. indexof ( " ; " );
10 Return Value. substring (starttag. length, indexend - Starttag. Length );
11 }
Note:
Cookies are sometimes in response. Cookies, and sometimes in response. headers ["Set-Cookie",
Each website has different practices. You need to combine HTTP/HTTPS packet capture tools (for example, httpanalyzerstdv4) to determine the encoding method.
This is also possible during debugging. During breakpoint debugging, you can use the monitor to view it.
The following method directly injects a cookie into the webbrowser (I do not know if this is true ).
The cookiecheckcode object is a cookiecollection object that contains the cookie information required to access the specified website. There may be multiple cookies.
// URL: URL
// Cookiename: Cookie name, for example:ASP. net_sessionid
// Cookievalue: cookie value; for example:62323de6d2de622af811bdf724f4ef7
// Call webbrowser1.refresh (webbrowserrefreshoption. Completely) after this method is executed.
Internetsetcookie (URL, cookiename, cookievalue );
1 [Dllimport ( " Wininet. dll " , Charset = Charset. Auto, setlasterror = True )]
2 Public Static Extern Bool Internetsetcookie ( String Lpszurlname, String Lbszcookiename, String Lpszcookiedata );
1 If (Webbrowser1.document ! = Null )
2 {
3 Foreach (Cookie ccc In Cookiecheckcode)
4 {
5Internetsetcookie (webbrowser1.document. url. absoluteuri, CCC. Name, CCC. value );
6}
7 }