Java Code Examples for org.apache.http.cookie.Cookie#getDomain()

The following examples show how to use org.apache.http.cookie.Cookie#getDomain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebFragment.java    From BigApp_Discuz_Android with Apache License 2.0 8 votes vote down vote up
@Override
    public void setCookieFromCookieStore(Context context, String url, List<Cookie> cks) {
        CookieUtils.syncCookie(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        if (!ListUtils.isNullOrContainEmpty(cks)) {
            for (int i = 0; i < cks.size(); i++) {
                Cookie cookie = cks.get(i);
                String cookieStr = cookie.getName() + "=" + cookie.getValue() + ";"
                        + "expiry=" + cookie.getExpiryDate() + ";"
                        + "domain=" + cookie.getDomain() + ";"
                        + "path=/";
//                ZogUtils.printError(WebFragment.class, "set cookie string:" + cookieStr);
                cookieManager.setCookie(url, cookieStr);//cookieStr是在HttpClient中获得的cookie

            }
        }
    }
 
Example 2
Source File: HC4ExchangeFormAuthenticator.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Authenticated httpClientAdapter (with cookies).
 *
 * @return http client
 */
public org.apache.commons.httpclient.HttpClient getHttpClient() throws DavMailException {
    org.apache.commons.httpclient.HttpClient oldHttpClient;
    oldHttpClient = DavGatewayHttpClientFacade.getInstance(url);
    DavGatewayHttpClientFacade.setCredentials(oldHttpClient, username, password);
    DavGatewayHttpClientFacade.createMultiThreadedHttpConnectionManager(oldHttpClient);

    for (Cookie cookie : httpClientAdapter.getCookies()) {
        org.apache.commons.httpclient.Cookie oldCookie = new org.apache.commons.httpclient.Cookie(
                cookie.getDomain(),
                cookie.getName(),
                cookie.getValue(),
                cookie.getPath(),
                cookie.getExpiryDate(),
                cookie.isSecure());
        oldCookie.setPathAttributeSpecified(cookie.getPath() != null);
        oldHttpClient.getState().addCookie(oldCookie);
    }

    return oldHttpClient;
}
 
Example 3
Source File: PersistentCookieStore.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    if (omitNonPersistentCookies && !cookie.isPersistent())
        return;
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 4
Source File: PersistentCookieStore.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 5
Source File: PersistentCookieStore.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    if (omitNonPersistentCookies && !cookie.isPersistent())
        return;
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 6
Source File: PersistentCookieStore.java    From sealtalk-android with MIT License 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 7
Source File: PersistentCookieStore.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    if (omitNonPersistentCookies && !cookie.isPersistent())
        return;
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 8
Source File: ClanBaseUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void printCookieStore(CookieStore cookieStore) {
    List<Cookie> cookies = cookieStore.getCookies();
    Log.e("APP", "========================================== start cookies.size:" + cookies.size());
    if (!cookies.isEmpty()) {
        for (int i = 0; i < cookies.size(); i++) {
            Cookie ck = cookies.get(i);
            String ckstr = ck.getName() + "=" + ck.getValue() + ";"
                    + "expiry=" + ck.getExpiryDate() + ";"
                    + "domain=" + ck.getDomain() + ";"
                    + "path=/";

            Log.v("APP", "cookieStr:" + ckstr);
        }
    }
    Log.e("APP", "========================================== end cookies.size:" + cookies.size());
}
 
Example 9
Source File: PersistentCookieStore.java    From Mobike with Apache License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    if (omitNonPersistentCookies && !cookie.isPersistent())
        return;
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 10
Source File: A6UserInfoSPUtil.java    From Huochexing12306 with Apache License 2.0 6 votes vote down vote up
public void saveCookies(List<Cookie> cookies){
	String strCookie = "";
       Date sessionTime = null;
       
       if (cookies != null && !cookies.isEmpty()) {
           for (int i = 0; i < cookies.size(); i++) {
           	Cookie cookie = cookies.get(i);
           	if (cookie.getName().equalsIgnoreCase("JSESSIONID")){
            	strCookie += cookie.getName() + "="
	                     + cookie.getValue() + ";domain="
	                     +cookie.getDomain();
	            sessionTime = cookies.get(i).getExpiryDate();
           	}
           }
       }
       editor.putString("cookies", strCookie);
	editor.commit();
	editor.putString("cookiesExpiryDate", (sessionTime == null)?null:TimeUtil.getDTFormat().format(sessionTime));
	editor.commit();
}
 
Example 11
Source File: PersistentCookieStore.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    String name = cookie.getName() + cookie.getDomain();

    // Save cookie into local store, or remove if expired
    if (!cookie.isExpired(new Date())) {
        cookies.put(name, cookie);
    } else {
        cookies.remove(name);
    }

    // Save cookie into persistent store
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
    prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
    prefsWriter.commit();
}
 
Example 12
Source File: SolrPortAwareCookieSpecFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void validate(final Cookie cookie, final CookieOrigin origin)
    throws MalformedCookieException {
  if (origin != null && origin.getHost() != null && cookie != null) {
    String hostPort = origin.getHost() + ":" + origin.getPort();
    String domain = cookie.getDomain();

    if (hostPort.equals(domain)) {
      return;
    }
  }
  super.validate(cookie, origin);
}
 
Example 13
Source File: SolrPortAwareCookieSpecFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
  if (origin != null && origin.getHost() != null && cookie != null) {
    String hostPort = origin.getHost() + ":" + origin.getPort();
    String domain = cookie.getDomain();
    if (hostPort.equals(domain)) {
      return true;
    }
  }
  return super.match(cookie, origin);
}
 
Example 14
Source File: HttpTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * @param cookieName name of cookie.
 * @return domain of cookie in the cookie store.
 */
public String cookieDomain(String cookieName) {
    String result = null;
    Cookie cookie = getCookie(cookieName);
    if (cookie != null) {
        result = cookie.getDomain();
    }
    return result;
}
 
Example 15
Source File: PersistentCookieStore.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Non-standard helper method, to delete cookie
 *
 * @param cookie cookie to be removed
 */
public void deleteCookie(Cookie cookie) {
    String name = cookie.getName() + cookie.getDomain();
    cookies.remove(name);
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.remove(COOKIE_NAME_PREFIX + name);
    prefsWriter.commit();
}
 
Example 16
Source File: PersistentCookieStore.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Non-standard helper method, to delete cookie
 *
 * @param cookie cookie to be removed
 */
public void deleteCookie(Cookie cookie) {
    String name = cookie.getName() + cookie.getDomain();
    cookies.remove(name);
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.remove(COOKIE_NAME_PREFIX + name);
    prefsWriter.commit();
}
 
Example 17
Source File: DefaultCookieManager.java    From esigate with Apache License 2.0 5 votes vote down vote up
private String toString(Cookie cookie) {
    StringBuilder result = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
    result.append(cookie.getName());
    result.append("=");
    result.append(cookie.getValue());
    if (cookie.getDomain() != null) {
        result.append(";domain=");
        result.append(cookie.getDomain());
    }
    if (cookie.getPath() != null) {
        result.append(";path=");
        result.append(cookie.getPath());
    }
    if (cookie.getExpiryDate() != null) {
        result.append(";expires=");
        result.append(cookie.getExpiryDate());
    }
    if (cookie.getCommentURL() != null) {
        result.append(";comment=");
        result.append(cookie.getComment());
    }
    if (cookie.getCommentURL() != null) {
        result.append(";comment=");
        result.append(cookie.getCommentURL());
    }
    return result.toString();
}
 
Example 18
Source File: PersistentCookieStore.java    From Mobike with Apache License 2.0 5 votes vote down vote up
/**
 * Non-standard helper method, to delete cookie
 *
 * @param cookie cookie to be removed
 */
public void deleteCookie(Cookie cookie) {
    String name = cookie.getName() + cookie.getDomain();
    cookies.remove(name);
    SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
    prefsWriter.remove(COOKIE_NAME_PREFIX + name);
    prefsWriter.commit();
}
 
Example 19
Source File: FusionPipelineClient.java    From storm-solr with Apache License 2.0 5 votes vote down vote up
protected synchronized void clearCookieForHost(String sessionHost) throws Exception {
  Cookie sessionCookie = null;
  for (Cookie cookie : cookieStore.getCookies()) {
    String cookieDomain = cookie.getDomain();
    if (cookieDomain != null) {
      if (sessionHost.equals(cookieDomain) ||
        sessionHost.indexOf(cookieDomain) != -1 ||
        cookieDomain.indexOf(sessionHost) != -1)
      {
        sessionCookie = cookie;
        break;
      }
    }
  }

  if (sessionCookie != null) {
    BasicClientCookie httpCookie =
      new BasicClientCookie(sessionCookie.getName(),sessionCookie.getValue());
    httpCookie.setExpiryDate(new Date(0));
    httpCookie.setVersion(1);
    httpCookie.setPath(sessionCookie.getPath());
    httpCookie.setDomain(sessionCookie.getDomain());
    cookieStore.addCookie(httpCookie);
  }

  cookieStore.clearExpired(new Date()); // this should clear the cookie
}
 
Example 20
Source File: HtmlUnitDomainHandler.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
    String domain = cookie.getDomain();
    if (domain == null) {
        return false;
    }

    final int dotIndex = domain.indexOf('.');
    if (dotIndex == 0 && domain.length() > 1 && domain.indexOf('.', 1) == -1) {
        final String host = origin.getHost();
        domain = domain.toLowerCase(Locale.ROOT);
        if (browserVersion_.hasFeature(HTTP_COOKIE_REMOVE_DOT_FROM_ROOT_DOMAINS)) {
            domain = domain.substring(1);
        }
        return host.equals(domain);
    }

    if (dotIndex == -1
            && !HtmlUnitBrowserCompatCookieSpec.LOCAL_FILESYSTEM_DOMAIN.equalsIgnoreCase(domain)) {
        try {
            InetAddress.getByName(domain);
        }
        catch (final UnknownHostException e) {
            return false;
        }
    }

    return super.match(cookie, origin);
}