有关httpClient获取sessionID的问题

qq_33550982 qq_33550982 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

public static void main(String[] args) {

HttpClient h = new HttpClient();
String sessionID = "";
try {
GetMethod get = new GetMethod("http://shop.gkyc.cn/login.jsp");
h.executeMethod(get);
Cookie[] cookie = h.getState().getCookies();
for(int i =0;i<cookie.length;i++){
if(cookie[i].getName().equals("JSESSIONID")){
sessionID = cookie[i].getValue();
System.out.println("第一次:::::::::::"+sessionID);
}
}
File f1 = new File("code1.png");
get = new GetMethod( "http://shop.gkyc.cn/vcodeimg.jsp" );
h.executeMethod(get);
InputStream in = get.getResponseBodyAsStream();
FileOutputStream out = new FileOutputStream(f1);
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!= -1){
out.write(b,0,len);
}

cookie = h.getState().getCookies();
for(int i =0;i<cookie.length;i++){
if(cookie[i].getName().equals("JSESSIONID")){
sessionID = cookie[i].getValue();
System.out.println("第二次:::::::::::"+sessionID);
}
}
System.out.println("开始调用uu打码:"+
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
boolean status=UUAPI.checkAPI(); //校验API,必须调用一次,校验失败,打码不成功
if(!status){
System.out.print("API文件校验失败,无法使用打码服务");
return;
}
String veridCode=UUAPI.easyDecaptcha("code1.png", 8002)[1];
System.out.println("uu打码成功("+veridCode+"):"+
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));



PostMethod post = new PostMethod("http://shop.gkyc.cn/login.jsp");

post.addParameter("userName", "wjszztrqyf");
post.addParameter("userPass", "wjszztrqyf");
post.addParameter("vcode", veridCode);
post.addParameter("loginBtn", "");

h.executeMethod(post);
cookie = h.getState().getCookies();
for(int i =0;i<cookie.length;i++){
if(cookie[i].getName().equals("JSESSIONID")){
sessionID = cookie[i].getValue();
System.out.println("第三次:::::::::::"+sessionID);
}
}

get = new GetMethod("http://shop.gkyc.cn/header.jsp");
/*if(sessionID!=null&&!sessionID.equals("")){
get.addRequestHeader("Cookie","JSESSIONID="+sessionID);
}*/
h.executeMethod(get);
cookie = h.getState().getCookies();
for(int i =0;i<cookie.length;i++){
if(cookie[i].getName().equals("JSESSIONID")){
sessionID = cookie[i].getValue();
System.out.println("第四次:::::::::::"+sessionID);
}
}
String response = get.getResponseBodyAsString();
//System.out.println(response);

if(response.contains("退出")){
System.out.println("OK");
}else{
System.out.println("false");
}

} catch (Exception e) {
e.printStackTrace();
}

}


控制台打印出来的sessionID 若登录成功 第2,3,4次的sessionID是一样的
为什么第一次get请求时 sessionID与后面几次的不同 都是同一个httpclient对象啊?
为什么第3次post请求的sessionID与第二次的相同啊
求告知啊 这httpClient是如何保持sessionID的
...全文
评论 1
收藏 已收藏
分享
返回顶部
qq_33550982
HTML5 43,737 成员 5,571 内容