有关httpClient获取sessionID的问题
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的
...全文
相关推荐
AutoUtil.rar
具体来说,我们可以在请求头中设置"Cookie"字段,值为之前获取的SessionID,这样服务器就会认为这个请求是有权限的。
C# 百度相册图片下载器源代码
这可能涉及到模拟表单提交,使用HttpClient发送POST请求,携带用户名和密码。成功登录后,可能需要保存Cookie或使用SessionId来保持会话状态,以便后续请求能正确访问用户的相册。
AndroidHttpSession
`AndroidHttpSession`这个实例主要关注的是如何在Android应用中利用HttpURLConnection实现会话(Session)保持,以处理登录状态和验证码获取等问题。
android cookie获取和设置,webView的cookie的同步
获取 Cookie 信息在使用 HttpClient 发送请求之前或之后,可以调用 `getCookieStore().getCookies()` 方法来获取当前存储的所有 Cookie。
Java使用HttpClient实现Post请求实例
执行POST请求:```javaHttpResponse response = httpClient.execute(post);```执行POST请求,并获取HttpResponse对象,它包含了服务器的响应信息


