我正在尝试登录一个站点并为该站点的某些页面提取 HTML 数据。问题是,站点的每个链接/页面都包含当前登录的会话 ID。例如:如果我右键单击任何链接并在新选项卡中打开,则 URL 如下所示:
http://02.iswhm.jp/admin/adm_user_search.php?sex=0& PHPSESSID=xsd6flqcccj24j5evv8ussp76mr1
在 JAVA 中,如果我不指定会话 id,则无法获取 html 数据。例如:
String url = "http://02.iswhm.jp/admin/adm_user_search_result.php";
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
nameValuePairs.add(new BasicNameValuePair("loginstatus[5]", "90"));
nameValuePairs.add(new BasicNameValuePair("loginstatus[6]", "99"));
nameValuePairs.add(new BasicNameValuePair("PHPSESSID", "xsd6flqcccj24j5evv8ussp76mr1"));
…………
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
new UsernamePasswordCredentials("xxx", "xxxxx"));
HttpPost httpget = new HttpPost(uri);
httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpget);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),"SJIS"));
HttpEntity entity = response.getEntity();
String line = "";
while ((line = rd.readLine()) != null) {
temp+=line+"\n";
}
如果我不指定 PHPSESSID,上面的代码将不起作用。
如何使用 JAVA 的 HTTP API 获取会话 ID?