简体   繁体   English

获取当前登录的会话ID

[英]Getting Session ID for current login

I am trying to log in to a site and extract HTML data for some of the pages of that site. 我正在尝试登录一个站点并提取该站点某些页面的HTML数据。 The problem is, each link/page of the site contains the session id for the current login. 问题是,站点的每个链接/页面都包含当前登录的会话ID。 For example: If I right-click the any link and OPEN IN NEW TAB, the url looks like: 例如:如果我右键单击任意链接,然后单击“打开新标签”,则该URL如下所示:

http://02.iswhm.jp/admin/adm_user_search.php?sex=0& PHPSESSID=xsd6flqcccj24j5evv8ussp76mr1 http://02.iswhm.jp/admin/adm_user_search.php?sex=0& PHPSESSID = xsd6flqcccj24j5evv8ussp76mr1

From JAVA, if I do not specify the session id, I cannot get html data. 在JAVA中,如果不指定会话ID,则无法获取html数据。 For example: 例如:

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";
}

The above code does not work if I dont specify the PHPSESSID. 如果我未指定PHPSESSID,则以上代码将不起作用。

How can I get the session ID using JAVA's HTTP API ? 如何使用JAVA的HTTP API获取会话ID?

I am not sure, this should be the proper way for it, but you can always get the cookie with name PHPSESSID , and the value would have the ID. 我不确定,这应该是正确的方法,但是您始终可以使用名称为PHPSESSIDcookie来获取cookie ,并且该值具有ID。 This is a kinda work-around. 这是一种解决方法。

Give it a try :-) 试试看 :-)

Thanks 谢谢

I am not sure, but you can use HttpServletRequest interface in which you can use getSession().getId(); 我不确定,但是可以使用HttpServletRequest接口,在其中可以使用getSession()。getId();。 you will get the session id. 您将获得会话ID。 I may be wrong. 我可能是错的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM