php httpclient cookie,httpclient模拟登陆具体实现(使用js设置cookie)

httpclient模拟登陆(使用js设置cookie)

代码如下:

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"); //这个网址对应的是window.location.host的返回值

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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值