我可以使用发送 POST 请求org.apache.http.clien.HttpClient
并获取 HTTP 响应。但是登录时我没有得到 HTML 内容,因为我的 PHP 脚本需要一个 cookie。那么,如何读取 POST 请求响应的 cookie 并在 POST 请求之后使用 GET 请求将其发回?
HttpClient httpClient = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "passwd"));
HttpPost httpPost = new HttpPost("http://localhost/index.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
BufferedInputStream bis = new BufferedInputStream(httpResponse.getEntity().getContent()); // Just gets the HTML content, not the cookies