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

网络编程 发布日期:2024/10/10 浏览次数:1

正在浏览:httpclient模拟登陆具体实现(使用js设置cookie)
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。
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。