java - how to obtain a JSESSIONID cookie -
i have android client , how doing request tomcat server:
protected string executerequest(string url) { basichttpparams httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparams, 10000); string output = "", line = ""; try { httpget getrequest = null; defaulthttpclient httpclient = new defaulthttpclient(); try { getrequest = new httpget(url); } catch(exception e) { e.printstacktrace(); return null; } getrequest.addheader("accept", "application/json"); httpresponse response = httpclient.execute(getrequest); if (response.getstatusline().getstatuscode() != 200) { response.getstatusline().getstatuscode(); return null; } bufferedreader br = new bufferedreader(new inputstreamreader((response.getentity().getcontent()))); while ((line = br.readline()) != null) { output += line; } httpclient.getconnectionmanager().shutdown(); } catch (clientprotocolexception e) { e.printstacktrace(); log.w(tag, e.getmessage()); } catch (illegalstateexception e) { e.printstacktrace(); log.w(tag, e.getmessage()); } catch (ioexception e) { e.printstacktrace(); log.w(tag, e.getmessage()); } if(output.equals("")) { output = null; } return output; }
now want able jsessionid cookie. understand need provide cookie explained here, how jsessionid
in first time?
thanks!
ok, here how did it. don't know if there easier way:
httpresponse response = httpclient.execute(getrequest); header[] headers = response.getheaders("set-cookie"); for(int = 0; < headers.length; i++) { if(headers[i].getname().equals("set-cookie")) { string pattern1 = "jsessionid="; string pattern2 = ";"; pattern p = pattern.compile(pattern.quote(pattern1) + "(.*?)" + pattern.quote(pattern2)); matcher m = p.matcher(headers[i].getvalue()); if(m.find()) { sessionid = m.group(1); break; } } }
Comments
Post a Comment