如何用HttpClient来操作Cookie我的代码是这样写的:public static void main(String[] args) throws IOException
{
HttpClient client = new HttpClient();
// Set Cookie
client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
HttpState initialState = new HttpState();
Cookie cookie = new Cookie();
cookie.setDomain("group.bj.chinamobile.com");
cookie.setPath("/");
cookie.setName("JSESSIONID");
cookie.setValue("0000_jiLQsJSN3bm1gh50AUoZgY:11e205r4h");
initialState.addCookie(cookie);
client.setState(initialState);
// End
client.getHostConfiguration().setHost("group.bj.chinamobile.com", 80,
"http");
HttpMethod method = getPostMethod(); // 使用 POST 方式提交数据
client.executeMethod(method); // 打印服务器返回的状态
System.out.println(method.getStatusLine()); // 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes(
"8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}private static HttpMethod getPostMethod()
{
PostMethod post = new PostMethod("/edsmp/LinkmanSave.do");
NameValuePair name = new NameValuePair("name", "Sotier_Yan");
NameValuePair mphone = new NameValuePair("mphone", "13566667777");
NameValuePair agroupid = new NameValuePair("agroupid", "22508551");
post.setRequestBody(new NameValuePair[]
{ name, mphone, agroupid });
return post;
}
------------这样写有一个问题,当我在浏览器退出以后,操作就不能成功了,也就是说 JSESSIONID 失效以后 就不行了
请问该如何解决?
用户名、密码 什么的怎么设置到cookie里?
{
HttpClient client = new HttpClient();
// Set Cookie
client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
HttpState initialState = new HttpState();
Cookie cookie = new Cookie();
cookie.setDomain("group.bj.chinamobile.com");
cookie.setPath("/");
cookie.setName("JSESSIONID");
cookie.setValue("0000_jiLQsJSN3bm1gh50AUoZgY:11e205r4h");
initialState.addCookie(cookie);
client.setState(initialState);
// End
client.getHostConfiguration().setHost("group.bj.chinamobile.com", 80,
"http");
HttpMethod method = getPostMethod(); // 使用 POST 方式提交数据
client.executeMethod(method); // 打印服务器返回的状态
System.out.println(method.getStatusLine()); // 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes(
"8859_1"));
// 打印返回的信息
System.out.println(response);
method.releaseConnection();
}private static HttpMethod getPostMethod()
{
PostMethod post = new PostMethod("/edsmp/LinkmanSave.do");
NameValuePair name = new NameValuePair("name", "Sotier_Yan");
NameValuePair mphone = new NameValuePair("mphone", "13566667777");
NameValuePair agroupid = new NameValuePair("agroupid", "22508551");
post.setRequestBody(new NameValuePair[]
{ name, mphone, agroupid });
return post;
}
------------这样写有一个问题,当我在浏览器退出以后,操作就不能成功了,也就是说 JSESSIONID 失效以后 就不行了
请问该如何解决?
用户名、密码 什么的怎么设置到cookie里?
楼主【sotier】截止到2008-07-21 16:42:42的历史汇总数据(不包括此帖):
发帖的总数量:28 发帖的总分数:480 每贴平均分数:17
回帖的总数量:30 得分贴总数量:4 回帖的得分率:13%
结贴的总数量:26 结贴的总分数:410
无满意结贴数:7 无满意结贴分:160
未结的帖子数:2 未结的总分数:70
结贴的百分比:92.86 % 结分的百分比:85.42 %
无满意结贴率:26.92 % 无满意结分率:39.02 %
值得尊敬