1

我正在尝试登录一个站点并为该站点的某些页面提取 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?

4

2 回答 2

1

我不确定,这应该是正确的方法,但是您始终可以获取cookiewith name PHPSESSID,并且该值将具有 ID。这是一种解决方法。

试试看 :-)

谢谢

于 2013-11-11T06:55:31.167 回答
0

我不确定,但您可以使用 HttpServletRequest 接口,您可以在其中使用 getSession().getId(); 您将获得会话 ID。我可能错了。

于 2013-11-11T07:10:03.657 回答