我可以使用org.apache.http.clien.HttpClient
發送POST請求並獲取HTTP響應。但是我登錄時不能獲取HTML內容,因爲我的PHP腳本需要cookie。那麼,如何在POST請求之後讀取POST請求響應的cookie並使用GET請求發回它?如何從POST請求中檢索cookie?
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
String cookie = httpResponse.getFirstHeader(「Cookie」)。getValue(); – 2017-05-16 18:56:52