Httpclient simulated login implementation
The simplest way is to customize an httpclient through the obtained cookie. If you are interested, you can understand this article.
Httpclient simulated login (set cookie using js)
The Code is as follows:
<Html>
<Meta http-equiv = 'content-type' Content = 'text/html; charset = gb2312 '>
<Link rel = stylesheet type = text/css href = '/bbs.css'>
<Script> document. cookie = 'utmpnum = 51; path =/; domain =. '+ window. location. host </script>
<Script> document. cookie = 'utmpkey = 20154732; path =/; domain =. '+ window. location. host </script>
<Script> document. cookie = 'utmpuserid = yay; path =/; domain =. '+ window. location. host </script>
<Meta http-equiv = 'refresh' content = '0; url =/firstpage. php'>
H4ttpclient 4.3:
The simplest way is to customize an httpclient through the cookie. Based on the example above, the method is as follows:
The Code is as follows:
CookieStore cookieStore = new BasicCookieStore ();
For (int I = 0; I <3; I ++ ){
String name;
String value;
Int flag = s. indexOf ("document. cookie ");
S = s. substring (flag + 17 );
Flag = s. indexOf ('= ');
Name = s. substring (0, flag );
Value = s. substring (flag + 1, s. indexOf (';'));
BasicClientCookie cookie = new BasicClientCookie (name,
Value );
Cookie. setVersion (0 );
Cookie. setDomain (".www.zju88.org"); // The URL corresponds to <span style = "font-family: Arial, Helvetica, sans-serif;"> window. location. host return value </span>
Cookie. setPath ("/");
CookieStore. addCookie (cookie );
}
// Set the store
CloseableHttpClient httpclient = HttpClients. custom ()
. Setdefacookcookiestore (cookieStore)
. Build ();
Android:
I haven't found the API corresponding to the above method for a long time. I can only add a cookie before each access.
The Code is as follows:
String cookie = "";
For (int I = 0; I <3; I ++ ){
String name;
String value;
Int flag = s. indexOf ("document. cookie ");
S = s. substring (flag + 17 );
Flag = s. indexOf ('= ');
Name = s. substring (0, flag );
Value = s. substring (flag + 1, s. indexOf (';'));
Cookie + = name + "=" + value;
If (I! = 2)
Cookie + = ";";
}
/*
* Network Access
*/
HttpGet httpget = new HttpGet (url );
Httpget. addHeader ("Cookie", cookie );
// If httpclient already has a cookie, you may need to set the httpclient cookie policy. For details, see the official API (:
HttpResponse response = httpclient.exe cute (httpget );