0
我需要使用Java與有狀態的http服務器交互來編寫Http客戶端。客戶端需要如何使用Java建立與遠程服務器的HttpClient交互
- 導航到登錄頁面,並接受cookies
- 提交登陸頁面,填寫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);
任何想法,我錯了嗎?或者你有更好的方法來實現這個?
非常感謝 綠色