如何用HttpClient来操作Cookie
如何用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里?
------解决方案--------------------你需要设置cookie的失效时间,没有设置的话,这个cookie是放在内存中的,你关闭浏览器就自动清除了。
------解决方案--------------------用HttpClient打开那个页 然后得到连接cookies再
中这个cookies提交就可以了吗
------解决方案--------------------不能!!!必须识别验证码(也许在cookies或session中能找到一些)放心吧!像识别验证码识别的高级程序是没有人愿意告诉你的 自学吧