0

我正在尝试从 http 响应获取 cookie。但我得到空输出。我检查了提琴手,http 响应包含一个 set-cookie 标头。但似乎 Cookie 存储中不存在 Cookie。显然我要去某个地方。我需要在哪里更正代码?请帮帮我。

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);
                    }
             }

         }

    }

}

 }
4

1 回答 1

0

我在 Stack 的第一个问题原来是正在使用的 Java 版本中的一个错误。 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8160038

于 2016-06-22T19:43:38.193 回答