Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7169142

CookieHandler does not work with localhost

XMLWordPrintable

    • b96
    • x86
    • windows_xp
    • Verified

        Bug Description:
        The java.net.CookieHandler does not work with localhost and any host name without a dot. The bug seems in the implementation of java.net.HttpCookie.domainMatch() or the usage in java.netCookiePolicy. ACCEPT_ORIGINAL_SERVER.

        Working host names:
                MyHost.local
                 127.0.0.1
                 any IPv4 address

        Not working host names:
                 MyHost
                 [::1]
                 any IPv6 address

        We see the problem with Java 6 and 7. It is not a regression.

        To run the follow code you need only a local http server which send a cookie. For example a session cookie.

                URL url = new URL( "http://localhost/ );
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                urlConnection.connect();
                urlConnection.getInputStream();
                if (newcookie != null) {
                    CookieHandler cookieHandler = CookieHandler.getDefault();
                    if (cookieHandler == null) {
                        cookieHandler = new java.net.CookieManager();
                        CookieHandler.setDefault( cookieHandler );
                    }
                    try {
                        cookieHandler.put( urlConnection.getURL().toURI(), urlConnection.getHeaderFields() );
                        Map<String, List<String>> map = cookieHandler.get( urlConnection.getURL().toURI(), urlConnection.getHeaderFields() );
                        if (map.containsKey( "Cookie" )) {
                            List<String> list = map.get( "Cookie" ); // The list is empty for localhost and host names without dot
                            if (list != null && list.size()>0) {
                                String element = list.get( 0 );
                                if (element != null && element.length()>0) {
                                    setCookie(element); // Use for the next request
                                }
                            }
                        }
                    } catch( URISyntaxException e ) {
                        e.printStackTrace();
                    }
                }

              khazra Kurchi Subhra Hazra
              tyao Ting-Yun Ingrid Yao (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: