小虾用httpclient模拟一个post包请求网站loginServlet获得了该网站返回的response信息
部分代码如下:
//建立连接
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
PostMethod method = new PostMethod("/StarMap/loginservlet?tag=login");
//传递username和userpassword模拟post方法传参给servlet
NameValuePair name = new NameValuePair("username", "xxxxxxx");
NameValuePair pass = new NameValuePair("userpassword", "xxxxxx");
NameValuePair[] namevaluepair = { name, pass };
//返回执行连接的状态
int status = client.executeMethod(method);//把得到的信息添加到response头中建立新的连接
Header locationHeader = method.getResponseHeader("Set-Cookie");

GetMethod method2 = new GetMethod("/StarMap/main/main.jsp");
method2.setRequestHeader(locationHeader);
client.executeMethod(method2);
String body=method2.getResponseBodyAsString();
System.out.println(body);控制台可以打印该jsp的源代码,由于该jsp中嵌套了其他的jsp,其在加载时需要对用户登录进行判断,到这时就会出现用户非法登录的信息
请问一下我该怎样在httpclient中获得cookie并保存到本地或会话过程中有效(服务期使用的是session会话保存信息,我这边不知到存的信息)

解决方案 »

  1.   

    一般来说,只要你用同一个client对象去访问,就能保证在同一个session生存期内。
      

  2.   

    楼上的我知道,可能我写的意思不明确,说明一下我的需求吧:项目a需要项目b的部分资源,但b中每一个页面都对用户进行验证,我怎样才能够在a中嵌套b的资源。因为需要登录所以
      

  3.   

    小弟初学网络,对cookie之类了解不多,望赐教
      

  4.   

    少加了一句 method.addParameters(namevaluepair);