httpclient simulates login (use js to set cookies)
Copy the code code 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 method is to customize an httpclient through the obtained cookie. According to the above example, the method is as follows:
Copy the code code 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"); //This URL corresponds to the return value of <span style="font-family: Arial, Helvetica, sans-serif;">window.location.host</ span>
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
// Set the store
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
android:
I searched for a long time and couldn't find the API corresponding to the above method. I can only add cookies before each visit.
Copy the code code 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+=";";
}
/*
* Every time you access the network
*/
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Cookie",cookie);
//If there are already cookies in httpclient, you may need to set the cookie policy of httpclient. For details, please check the official API (:
HttpResponse response = httpclient.execute(httpget);
ps: If you are using java SE, you can also use the htmlunit class, which will execute js.