HttpClient post 提交数据问题
我想用HttpClient 模拟用户登录,但是总是不能返回登录以后的界面(我有该网站的账号,因此用户名和密码都是正确的)
代码:
public static void main(String[] args) throws Exception {
HttpClient httpClient = new HttpClient();
PostMethod getMethod = new PostMethod(
"http://localhost:8080/login.jsp");
getMethod.setParameter("name", URLEncoder.encode("姓名 ", "gb2312"));
getMethod.setParameter("password", "2222");
httpClient.executeMethod(getMethod);
System.out.println(getMethod.getResponseBodyAsString());
}
这段代码打印的信息时登录界面,这是为什么?
[解决办法]
PostMethod p = new PostMethod("http://xxxx.com");// 参数 NameValuePair email = new NameValuePair("email","xxxxxxxxxxx@126.com");NameValuePair password = new NameValuePair("password", "xxxxxx");NameValuePair[] params = new NameValuePair[] { email, password };p.setRequestBody(params);int status = c.executeMethod(p);System.out.println(p.getResponseBodyAsString());
[解决办法]