我需要使用与有状态http服务器交互的java编写Http客户端。客户需要
我正在尝试使用HttpClient来实现此客户端。但是我发现即使我提交了登录表单,它仍然会返回登录表单,就像我的提交无效一样。这是我的代码:
HttpClient agent = new DefaultHttpClient();
agent.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.RFC_2965);
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet(site);
HttpResponse response = agent.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
entity.getContent().close();
HttpPost post = new HttpPost(site + "/login.aspx");
post.getParams().setParameter("LoginControl1$ctlLoginName", "myusername");
post.getParams().setParameter("LoginControl1$ctlPassword", "mypassword");
response = agent.execute(post, localContext);
entity = response.getEntity();
String s = IO.readContentAsString(entity.getContent());
System.out.println(s);
知道我错了吗?或者你有更好的方法来实现这个吗?
非常感谢 绿色
答案 0 :(得分:0)
不确定可能出现的问题,但您是否考虑过使用tcpdump / Wireshark来获取与服务器的原始HTTP对话,并将您在HTTPClient中发送/接收的内容与您在Web浏览器中发送/接收的内容进行比较你提交表格吗?