:
서버가 단일 요청에 여러 개의 쿠키를 설정할 수 있기 때문에, 우리가 찾고, 응답 헤더를 통해 루프가 필요합니다 모든 머리글은 "Set-Cookie"로 명명됩니다.
String headerName=null;
for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = urlConn.getHeaderField(i);
의 getHeaderField (INT 인덱스)에 의해 반환 된 문자열있어서 세미콜론으로 분리 된 이름 = 값의 연속이다(). 첫 번째 이름/값 쌍은 우리가 관심있는 실제 데이터 문자열 (예 : "sessionId = 0949eeee22222rtg")이며, 후속 이름/값 쌍은 쿠키의 저장소를 관리하는 데 사용할 메타 정보입니다 (만료시 등).).
cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
이것은 기본적으로입니다. 이제 쿠키 이름 (cookieName)과 쿠키 값 (cookieValue)이 있습니다. 당신은 당신의 이름이 무엇인지 알 필요에 따라 어떤 방식으로
public void getCookieUsingCookieHandler() {
try {
// Instantiate CookieManager;
// make sure to set CookiePolicy
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
// get content from URLConnection;
// cookies are set by web site
URL url = new URL("http://host.example.com");
URLConnection connection = url.openConnection();
connection.getContent();
// get cookies from underlying
// CookieStore
CookieStore cookieJar = manager.getCookieStore();
List <HttpCookie> cookies =
cookieJar.getCookies();
for (HttpCookie cookie: cookies) {
System.out.println("CookieHandler retrieved cookie: " + cookie);
}
} catch(Exception e) {
System.out.println("Unable to get cookie using CookieHandler");
e.printStackTrace();
}
}
:
위의 설명이 http://www.hccp.org/java-net-cookie-how-to.html#retrieving_cookies
더 정교한 접근 방식에서 제공하는 예제를 기반으로 한이 CookieAccessor 클래스를 사용하는 것입니다 세션 ID를 보유한 쿠키로 값을 얻을 수 있습니다. 예를 들어 자바 웹 응용 프로그램은 일반적으로 이름이 "JSESSIOINID"인 쿠키를 만듭니다.