栏目:Web开发 作者:admin 日期:2015-05-02 评论:0 点击: 5,428 次
下面说一下,如何获取JSESSIONID,使用的主要工具为看HttpClient,请看下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
String url = ""; String refer; HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.USER_AGENT, ""); HttpMethod method = new GetMethod(url); client.executeMethod(method); url = method.getResponseBodyAsString(); method = new GetMethod(url); client.executeMethod(method); String sessionId = ""; Header[] headers = method.getResponseHeaders(); String cookie = null; for (Header h : headers) { if ("set-cookie".equalsIgnoreCase(h.getName())) { cookie = h.getValue(); break; } } String s = null; if (cookie != null) { StringTokenizer tz = new StringTokenizer(cookie, ";"); while (tz.hasMoreTokens()) { s = tz.nextToken().trim(); String[] array = s.split("="); if (array.length == 2) { if ("JSESSIONID".equalsIgnoreCase(array[0])) { sessionId = array[1]; break; } } } } |
------====== 本站公告 ======------
金丝燕网,一个严谨的网站!