HttpClient 会话维持的问题

notepads 2011-12-14 11:15:53
初步了解了一下HttpClient,需求是对一个系统进行登陆,然后下载指定URL的内容

PostMethod post = new PostMethod( "xxxxx/checkLogin.aspx" );
但对第二个地址进行连接的时候
post = new PostMethod( "xxxxx/downLoad.aspx?file=xxx" );

这样会话就丢失了,不知道这个怎么弄了,因为比较急,没有时间多研究。所以请高手指教了。
网上说把cookie再发回去,试了。不管用
...全文
330 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fainfy 2011-12-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 notepads 的回复:]
引用 4 楼 zyz1985 的回复:

httpclient会自动维护cookie,只要是用的一个httpclient就应该可以的吧.....

访问第二个地址的时候,现在是new了一下,
[/Quote]

一个HttpClient对象,就可以理解为浏览器的一个进程。如果是同一个进程后续的请求,Cookie应该会自动发过去的。

如果你重新又创建了一个新的HttpClient这时就相当于开了2个浏览器进程。2个进程是不相关的。
notepads 2011-12-14
  • 打赏
  • 举报
回复

[Quote=引用 4 楼 zyz1985 的回复:]

httpclient会自动维护cookie,只要是用的一个httpclient就应该可以的吧.....
[/Quote]
访问第二个地址的时候,现在是new了一下,
idlqy 2011-12-14
  • 打赏
  • 举报
回复
我也是直接使用一个httpclient进行操作,没问题啊.学习了.
dracularking 2011-12-14
  • 打赏
  • 举报
回复
cookie不管用的话有很多其它原因的还要具体分析
比如cookie的域等是否与请求地址匹配,网站维持会话的方式是否非session cookie,用referer之类的
游一游走一走 2011-12-14
  • 打赏
  • 举报
回复
httpclient会自动维护cookie,只要是用的一个httpclient就应该可以的吧.....
dracularking 2011-12-14
  • 打赏
  • 举报
回复
获取上一次请求的session cookie后,要把这个session cookie随下一次请求一起发送以维持会话

session cookie可以通过加在Cookie这个头域中发送(httpclient通过cookie store来管理)。

HttpPost httppost = new HttpPost(postData);
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());
//cookie.setDomain("your domain");cookie.setPath("/");

cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
response = client.execute(httppost);

iori20099 2011-12-14
  • 打赏
  • 举报
回复
可以用httpLook来监控下
iori20099 2011-12-14
  • 打赏
  • 举报
回复
HttpPost post=new HttpPost(""xxxxx/downLoad.aspx?file=xxx""+"&"+sessionID);
加上这个sessionId;
SessionId的获得

HttpContext localContext = new BasicHttpContext();
--放入post对象和context对象获得response
HttpResponse _response2 = httpclient.execute(post, localContext);
CookieStore store2 = httpclient.getCookieStore();
List list2 = store2.getCookies();
String sessionId = "";
for (int i = 0; i < list2.size(); i++) {
Cookie cookie = (Cookie) list2.get(i);
String cookieName = cookie.getName();
if (cookieName.equals("JSESSIONID")) {
sessionId = cookie.getValue();
break;
}
}
notepads 2011-12-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iori20099 的回复:]

HttpPost post=new HttpPost(""xxxxx/downLoad.aspx?file=xxx""+"&"+sessionID);
加上这个sessionId;
SessionId的获得
Java code

HttpContext localContext = new BasicHttpContext();
--放入post对象和context对象获得re……
[/Quote]

请问一下,你用的是那个版本的,
Java的世界里,HttpClient 是一个功能强大的Http请求库,然而接口非常复杂,设计上遵从正交性,简单的请求也需要写比较多的代码,更不要说隐藏在各种细节里面的高级用法了。Requests,  是一个模仿python requests 模块来设计的Http lib,拥有简单而灵活的API,在容易使用的同时,又能够满足各种高级定制的使用,可是说是当前最好用的Java Http Client Lib。 简单的请求示例:String url = ...; Response resp = Requests.get(url).text(); // post 和其他方法 resp = Requests.post(url).text(); resp = Requests.head(url).text(); //读取Http Response  int statusCode = resp.getStatusCode(); Headers headers = resp.getHeaders(); Cookies cookies = resp.getCookies(); String body = resp.getBody(); //response 返回其他类型 resp = Requests.get(url).text("UTF-8"); // get response as bytes Response resp1 = Requests.get(url).bytes(); // save response as file  Response resp2 = Requests.get(url).file("/path/to/save/file"); // url 参数: Map map = new HashMap(); map.put("k1", "v1"); map.put("k2", "v2"); Response resp = Requests.get(url).param("key1", "value1").params(map)         //.params(new Parameter(...), new Parameter(...))         .text(); // 请求头 Response resp = Requests.get(url).header("key1", "value1").headers(map)         //.headers(new Header(...), new Header(...))         .text(); // 添加Cookie: Map cookies = new HashMap(); Response resp = Requests.get(url).cookie("key1", "value1").cookies(map)         //.cookies(new Cookie(...), new Cookie(...))         .text(); //  设置 userAgent Response resp = Requests.get(url).userAgent(userAgent).text(); // 增加请求数据(post, put, patch方法) // send form-encoded data. x-www-form-urlencoded header will be send automatically Response resp = Requests.post(url).data(map).text(); // send string data String str = ...; resp = Requests.post(url).data(str, "UTF-8").text(); // send from inputStream InputStream in = ... resp = Requests.post(url).data(in).text(); // multipart 请求, 用于文件上传: Response resp = Requests.post(url).data(map).multiPart("ufile", "/path/to/file")         .multiPart(..., ...).text();请求设置://禁止自动重定向

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧