httpclient模擬登陸(使用js設(shè)置cookie) 
復(fù)制代碼 代碼如下:
 
<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'> 
復(fù)制代碼 代碼如下:
 
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("."); //這個(gè)網(wǎng)址對(duì)應(yīng)的是<span>window.location.host的返回值</span> 
cookie.setPath("/"); 
cookieStore.addCookie(cookie); 
} 
// Set the store 
CloseableHttpClient httpclient = HttpClients.custom() 
.setDefaultCookieStore(cookieStore) 
.build(); 
復(fù)制代碼 代碼如下:
 
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+=";"; 
} 
/* 
* 每次訪問網(wǎng)絡(luò) 
*/ 
HttpGet httpget = new HttpGet(url); 
httpget.addHeader("Cookie",cookie); 
//如果httpclient中已經(jīng)有cookie可能需要設(shè)置httpclient的cookie策略,具體可查官方API (: 
HttpResponse response = httpclient.execute(httpget); 
新聞熱點(diǎn)
疑難解答
圖片精選