public void setCookies(URLConnection conn) throws IOException {

    // let's determine the domain and path to retrieve the appropriate cookies
    URL url = conn.getURL();
    String domain = getDomainFromHost(url.getHost());
    if (domain.equals("localhost")) {
      domain = "linkedin.com";
    }
    String path = url.getPath();

    Map<String, Map<String, String>> domainStore = store.get(domain);
    if (domainStore == null)
      return;
    StringBuilder cookieStringBuffer = new StringBuilder();

    Iterator<String> cookieNames = domainStore.keySet().iterator();
    while (cookieNames.hasNext()) {
      String cookieName = cookieNames.next();
      Map<String, String> cookie = domainStore.get(cookieName);
      // check cookie to ensure path matches and cookie is not expired
      // if all is cool, add cookie to header string
      if (comparePaths((String) cookie.get(PATH), path)
          && isNotExpired(cookie.get(EXPIRES))) {
        cookieStringBuffer.append(cookieName);
        cookieStringBuffer.append("=");
        cookieStringBuffer.append(cookie.get(cookieName));
        if (cookieNames.hasNext())
          cookieStringBuffer.append(SET_COOKIE_SEPARATOR);
      }
    }
    try {
      conn.setRequestProperty(COOKIE, cookieStringBuffer.toString());
    } catch (java.lang.IllegalStateException ise) {
      IOException ioe =
          new IOException(
              "Illegal State! Cookies cannot be set on a URLConnection that is already connected. "
                  + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect().");
      throw ioe;
    }
  }
public void setCookies ( URLConnection conn ) throws IOException { URL url = conn . getURL ( ) ; String domain = getDomainFromHost ( url . getHost ( ) ) ; if ( domain . equals ( "localhost" ) ) { domain = "linkedin.com" ; } String path = url . getPath ( ) ; Map < String , Map < String , String > > domainStore = store . get ( domain ) ; if ( domainStore == null ) return ; StringBuilder cookieStringBuffer = new StringBuilder ( ) ; Iterator < String > cookieNames = domainStore . keySet ( ) . iterator ( ) ; while ( cookieNames . hasNext ( ) ) { String cookieName = cookieNames . next ( ) ; Map < String , String > cookie = domainStore . get ( cookieName ) ; if ( comparePaths ( ( String ) cookie . get ( PATH ) , path ) && isNotExpired ( cookie . get ( EXPIRES ) ) ) { cookieStringBuffer . append ( cookieName ) ; cookieStringBuffer . append ( "=" ) ; cookieStringBuffer . append ( cookie . get ( cookieName ) ) ; if ( cookieNames . hasNext ( ) ) cookieStringBuffer . append ( SET_COOKIE_SEPARATOR ) ; } } try { conn . setRequestProperty ( COOKIE , cookieStringBuffer . toString ( ) ) ; } catch ( java . lang . IllegalStateException ise ) { IOException ioe = new IOException ( "Illegal State! Cookies cannot be set on a URLConnection that is already connected. " + "Only call setCookies(java.net.URLConnection) AFTER calling java.net.URLConnection.connect()." ) ; throw ioe ; } }
Prior to opening a URLConnection, calling this method will set all unexpired cookies that match
the path or subpaths for thi underlying URL

The connection MUST NOT have been opened method or an IOException will be thrown.

@param conn a java.net.URLConnection - must NOT be open, or IOException will be thrown
@throws java.io.IOException Thrown if conn has already been opened.