Android 获取JSP或ASP的sessionId(Cookie)
如果使用的是httpClient:
try { HttpResponse response = HttpUtil.httpClient.execute(new HttpGet(url)); Header header = response.getFirstHeader("Set-Cookie"); if (header != null) { String cookies = header.getValue(); String[] strings = cookies.split(";"); GlobalData.aspSessionScsbaabr = strings[0]; System.out.println("已经取到session = " + GlobalData.aspSessionScsbaabr); } } catch (IOException e) { e.printStackTrace(); }
如果使用HttpUrlConnection:
try { System.out.println("正在发送"); URL url = new URL(doLoginUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); InputStream inputStream = urlConnection.getInputStream(); String session_value = urlConnection.getHeaderField("Set-Cookie"); sessionId = session_value.split(";")[0]; System.out.println("sessionId = " + sessionId); byte[] byte_result = new byte[inputStream.available()]; inputStream.read(byte_result); String result = new String(byte_result); System.out.println("返回结果 result = " + result); inputStream.close(); } catch (Exception e) { e.printStackTrace(); }
本文出自 无忧之路 - 博客园