国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

httpclient模擬登陸具體實現(使用js設置cookie)

2019-11-20 21:29:09
字體:
來源:轉載
供稿:網友
httpclient模擬登陸(使用js設置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:

最簡單的方法就是通過得到的cookie定制一個httpclient,根據上面的例子,方法如下:
復制代碼 代碼如下:

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"); //這個網址對應的是<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:

我找了半天也沒找到上面方法對應的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+=";";
}

/*
* 每次訪問網絡
*/
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Cookie",cookie);
//如果httpclient中已經有cookie可能需要設置httpclient的cookie策略,具體可查官方API (:
HttpResponse response = httpclient.execute(httpget);

ps:如果是用java SE,也可以使用htmlunit類,它會執行js。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 白朗县| 和政县| 吴忠市| 璧山县| 东乡县| 温宿县| 花垣县| 邯郸县| 内乡县| 汶上县| 东莞市| 金堂县| 潜江市| 漯河市| 罗定市| 苏尼特左旗| 鸡西市| 余姚市| 稻城县| 嘉祥县| 滨州市| 万山特区| 阿合奇县| 荆州市| 远安县| 青岛市| 永定县| 镇赉县| 阳春市| 西畴县| 任丘市| 南涧| 彩票| 通江县| 甘泉县| 罗田县| 洪江市| 当雄县| 彰化市| 静乐县| 涟水县|