2016-06-18 8 views
0

http 응답에서 쿠키를 가져 오려고합니다. 하지만 빈 출력이 나옵니다. 내가 바이올린과 함께 체크, http 응답을 설정 쿠키 헤더가 포함되어 있습니다. 그러나 쿠키는 쿠키 저장소에없는 것으로 보입니다. 분명히 어딘가에 가고있다. 코드를 수정해야하는 곳은 어디입니까? 제발 도와주세요. 스택에서 누가 내 쿠키를 먹었습니까? 그들은 머리말이나 요리 스토어에 나타나지 않습니다. http 응답에서 쿠키를 읽으려고합니다.

public class Main { 

     public static void main(String[] args) throws Exception { 

     URL url = new URL("http://www.angelvestgroup.com/info.php?id=1"); 
     CookieManager manager = new CookieManager(); 
     manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 

     CookieHandler.setDefault(manager); 
     HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 

     Map<String, List<String>> headerFields = conn.getHeaderFields(); 

     Set<String> headerFieldsSet = headerFields.keySet(); 
     Iterator<String> hearerFieldsIter = headerFieldsSet.iterator(); 

     while (hearerFieldsIter.hasNext()) { 

     String headerFieldKey = hearerFieldsIter.next(); 

     if ("Set-Cookie".equalsIgnoreCase(headerFieldKey)) { 

      List<String> headerFieldValue=headerFields.get(headerFieldKey); 

      for (String headerValue : headerFieldValue) { 

       System.out.println("Cookie Found..."); 

       String[] fields = headerValue.split(";\\s*"); 

       String cookieValue = fields[0]; 
       String expires = null; 
       String path = null; 
       String domain = null; 
       boolean secure = false; 

       // Parse each field 
       for (int j = 1; j < fields.length; j++) { 
        if ("secure".equalsIgnoreCase(fields[j])) { 
         secure = true; 
        } 
        else if (fields[j].indexOf('=') > 0) { 
         String[] f = fields[j].split("="); 
         if ("expires".equalsIgnoreCase(f[0])) { 
          expires = f[1]; 
         } 
         else if ("domain".equalsIgnoreCase(f[0])) { 
          domain = f[1]; 
         } 
         else if ("path".equalsIgnoreCase(f[0])) { 
          path = f[1]; 
         } 
        } 

       } 

       System.out.println("cookieValue:" + cookieValue); 
       System.out.println("expires:" + expires); 
       System.out.println("path:" + path); 
       System.out.println("domain:" + domain); 
       System.out.println("secure:" + secure); 

         System.out.println("*****************************************"); 

       CookieStore cookieJar = manager.getCookieStore(); 

       List <HttpCookie> cookies = 
         cookieJar.get(url.toURI()); 
        for (HttpCookie cookie: cookies) { 
         System.out.println("CookieHandler retrieved cookie: " + cookie); 
        } 
      } 

     } 

    } 

} 

} 
+0

"누가 내 쿠키를 먹었습니까?" 나는 그것을 좋아한다. :-D –

+0

네, 유머없이 할 수 없습니다. 감사. –

+0

아마도 지루하고 유머가 없지만 들여 쓰기가 잘되면 특정 프로그래밍 오류를 발견하고 문제를 해결하는 것이 더 쉬울 것입니다. 대부분의 도구는 들여 쓰기를 자동으로 수정할 수 있습니다. – Roland

답변