httpclient模擬登陸具體實(shí)現(xiàn)(使用js設(shè)置cookie)
更新時(shí)間:2013年12月11日 17:12:55 作者:
最簡(jiǎn)單的方法就是通過得到的cookie定制一個(gè)httpclient,感興趣的朋友可以了解下本文
httpclient模擬登陸(使用js設(shè)置cookie)
<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:
最簡(jiǎn)單的方法就是通過得到的cookie定制一個(gè)httpclient,根據(jù)上面的例子,方法如下:
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"); //這個(gè)網(wǎng)址對(duì)應(yīng)的是<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:
我找了半天也沒找到上面方法對(duì)應(yīng)的API,只能在每次訪問前加cookie了
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);
ps:如果是用java SE,也可以使用htmlunit類,它會(huì)執(zhí)行js。
復(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'>
h4ttpclient 4.3:
最簡(jiǎn)單的方法就是通過得到的cookie定制一個(gè)httpclient,根據(jù)上面的例子,方法如下:
復(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(".www.zju88.org"); //這個(gè)網(wǎng)址對(duì)應(yīng)的是<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:
我找了半天也沒找到上面方法對(duì)應(yīng)的API,只能在每次訪問前加cookie了
復(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);
ps:如果是用java SE,也可以使用htmlunit類,它會(huì)執(zhí)行js。
您可能感興趣的文章:
- jquery實(shí)現(xiàn)界面無刷新加載登陸注冊(cè)
- struts2+jquery實(shí)現(xiàn)ajax登陸實(shí)例詳解
- jQuery的cookie插件實(shí)現(xiàn)保存用戶登陸信息
- 使用jQuery插件創(chuàng)建常規(guī)模態(tài)窗口登陸效果
- jQuery登陸判斷簡(jiǎn)單實(shí)現(xiàn)代碼
- 將jQuery應(yīng)用于login頁面的問題及解決
- Jsp中解決session過期跳轉(zhuǎn)到登陸頁面并跳出iframe框架的方法
- ExtJs 表單提交登陸實(shí)現(xiàn)代碼
- JS簡(jiǎn)單實(shí)現(xiàn)登陸驗(yàn)證附效果圖
- jQuery+Pdo編寫login登陸界面
相關(guān)文章
ExtJs中g(shù)ridpanel分組后組名排序?qū)嵗a
這篇文章主要介紹了ExtJs中g(shù)ridpanel分組后組名排序?qū)嵗a,有需要的朋友可以參考一下2013-12-12
JavaScript直播評(píng)論發(fā)彈幕切圖功能點(diǎn)集合效果代碼
這篇文章主要介紹了JavaScript直播評(píng)論發(fā)彈幕切圖功能點(diǎn)集合效果代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
JS實(shí)現(xiàn)根據(jù)當(dāng)前文字選擇返回被選中的文字
這篇文章主要介紹JS如何實(shí)現(xiàn)根據(jù)當(dāng)前文字選擇返回被選中的文字,需要的朋友可以參考下2014-05-05
使用JavaScript和MQTT開發(fā)物聯(lián)網(wǎng)應(yīng)用示例解析
這篇文章主要介紹了使用JavaScript和MQTT開發(fā)物聯(lián)網(wǎng)應(yīng)用示例解析,文章通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

