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

The following examples show how to use org.apache.http.cookie.Cookie#getName() . 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: DefaultCookieManager.java    From esigate with Apache License 2.0 6 votes vote down vote up
@Override
public List<Cookie> getCookies(DriverRequest originalRequest) {
    BasicCookieStore cookies = new BasicCookieStore();
    UserContext userContext = originalRequest.getUserContext();

    // Read cookies from session
    BasicCookieStore sessionCookies = (BasicCookieStore) userContext.getAttribute(COOKIES_LIST_SESSION_KEY);
    if (sessionCookies != null) {
        for (Cookie c : sessionCookies.getCookies()) {
            cookies.addCookie(c);
        }
    }

    // Read cookie from request
    Cookie[] requestCookies = originalRequest.getOriginalRequest().getCookies();
    if (requestCookies != null) {
        for (Cookie cookie : requestCookies) {
            String name = cookie.getName();
            if (!storeCookiesInSession.contains(name) && !storeCookiesInSession.contains("*")
                    && !discardCookies.contains(name) && !discardCookies.contains("*")) {
                cookies.addCookie(rewriteForServer(cookie, originalRequest));
            }
        }
    }
    return cookies.getCookies();
}
 
Example 3
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 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 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 6
Source File: CookieUtils.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static void setCookieFromCookieStore(Context context, String url) {
    syncCookie(context);

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    List<Cookie> cookies = getCookies(context);
    Log.e("cookies", "cookies.size:" + cookies.size());
    if (!cookies.isEmpty()) {
        for (int i = 0; i < cookies.size(); i++) {
            Cookie cookie = cookies.get(i);
            String cookieStr = cookie.getName() + "=" + cookie.getValue() + ";"
                    + "expiry=" + cookie.getExpiryDate() + ";"
                    + "domain=" + cookie.getDomain() + ";"
                    + "path=/";

            cookieManager.setCookie(url, cookieStr);//cookies是在HttpClient中获得的cookie

        }
    }

}
 
Example 7
Source File: PersistentCookieStore.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
	String name = cookie.getName();

	// 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 = this.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: 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 9
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 10
Source File: PreferencesCookieStore.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
@Override
public void addCookie(Cookie cookie) {
    String name = cookie.getName();

    // 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 11
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 12
Source File: SendSpaceUploaderPlugin.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
public static void loginSendSpace() throws Exception {
        HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        System.out.println("Trying to log in to sendspace");
        HttpPost httppost = new HttpPost("http://www.sendspace.com/login.html");
        httppost.setHeader("Cookie", sidcookie + ";" + ssuicookie);
//        httppost.setHeader("Referer", "http://www.filesonic.in/");
//        httppost.setHeader("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("action", "login"));
        formparams.add(new BasicNameValuePair("submit", "login"));
        formparams.add(new BasicNameValuePair("target", "%252F"));
        formparams.add(new BasicNameValuePair("action_type", "login"));
        formparams.add(new BasicNameValuePair("remember", "1"));
        formparams.add(new BasicNameValuePair("username", ""));
        formparams.add(new BasicNameValuePair("password", ""));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);

        System.out.println("Getting cookies........");
        Iterator<Cookie> it = httpclient.getCookieStore().getCookies().iterator();
        Cookie escookie = null;
        while (it.hasNext()) {
            escookie = it.next();
            if (escookie.getName().equalsIgnoreCase("ssal")) {
                ssalcookie = escookie.getName() + "=" + escookie.getValue();
                System.out.println(ssalcookie);
                login = true;
            }

        }

    }
 
Example 13
Source File: Session.java    From sndml3 with MIT License 5 votes vote down vote up
public String getSessionID() {
	for (Cookie cookie : cookieStore.getCookies() ) {
		String name = cookie.getName();
		String value = cookie.getValue();
		if ("JSESSIONID".equals(name)) return value;
	}
	return null;
}
 
Example 14
Source File: PersistentCookieStore.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public void deleteCookie(Cookie cookie)
{
    String s = cookie.getName();
    f.remove(s);
    android.content.SharedPreferences.Editor editor = g.edit();
    editor.remove((new StringBuilder()).append("cookie_").append(s).toString());
    editor.commit();
}
 
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: CookieUtils.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get a cookie from the name.
 * @param httpContext HttpContext in which the cookies store.
 * @param name the name of the cookie.
 * @return the Cookie object.
 */
public static Cookie getCookie(HttpContext httpContext, String name){
    CookieStore cookieStore = (CookieStore) httpContext.getAttribute(ClientContext.COOKIE_STORE);
    List<Cookie> cookies = cookieStore.getCookies();
    String cookieName;
    Cookie cookie;
    for(int i = 0; i < cookies.size(); i++){
        cookie = cookies.get(i);
        cookieName = cookie.getName();
        if(cookieName.contains(name)){
            return cookie;
        }
    }
    return null;
}
 
Example 17
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 18
Source File: ZidduUploaderPlugin.java    From neembuu-uploader with GNU General Public License v3.0 4 votes vote down vote up
public static void loginZiddu() throws Exception {



        HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);

        System.out.println("Trying to log in to ziddu");
        HttpPost httppost = new HttpPost("http://www.ziddu.com/login.php");
        httppost.setHeader("Referer", "http://www.ziddu.com/");
        httppost.setHeader("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
//        httppost.setHeader("Cookie", cfduidcookie);
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("email", "[email protected]"));
        formparams.add(new BasicNameValuePair("password", ""));

        formparams.add(new BasicNameValuePair("action", "LOGIN"));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);

        System.out.println("Getting cookies........");
        System.out.println(httpresponse.getStatusLine());
        Header[] allHeaders = httpresponse.getAllHeaders();
        for (int i = 0; i < allHeaders.length; i++) {
            System.out.println(allHeaders[i].getName() + " = " + allHeaders[i].getValue());
        }
        Iterator<Cookie> it = httpclient.getCookieStore().getCookies().iterator();
        Cookie escookie = null;
        while (it.hasNext()) {
            escookie = it.next();
//            System.out.println(escookie.getName() + " = " + escookie.getValue());
            if (escookie.getName().contains("PHPSESSID")) {
                phpsessioncookie = escookie.getName() + " = " + escookie.getValue();

                //  System.out.println("session cookie : " + sessioncookie);
                //}
            }

//        if (httpresponse.getStatusLine().getStatusCode() == 302) {
//            login = true;
//            System.out.println("localhostr Login Success");
//        } else {
//            System.out.println("localhostr Login failed");
//        }

//        System.out.println(EntityUtils.toString(httpresponse.getEntity()));


            InputStream is = httpresponse.getEntity().getContent();
            is.close();

            System.out.println("php session cookie : " + phpsessioncookie);
        }
    }
 
Example 19
Source File: DefaultCookieManager.java    From esigate with Apache License 2.0 4 votes vote down vote up
protected static Cookie rewriteForBrowser(Cookie cookie, DriverRequest request) {
    String name = cookie.getName();
    // Rewrite name if JSESSIONID because it will interfere with current
    // server session
    if ("JSESSIONID".equalsIgnoreCase(name)) {
        name = "_" + name;
    }

    // Rewrite domain
    String domain =
            rewriteDomain(cookie.getDomain(), request.getBaseUrl().getHost(),
                    UriUtils.extractHostName(request.getOriginalRequest().getRequestLine().getUri()));

    // Rewrite path
    String originalPath = cookie.getPath();
    String requestPath = UriUtils.getPath(request.getOriginalRequest().getRequestLine().getUri());
    String path = originalPath;
    if (requestPath == null || !requestPath.startsWith(originalPath)) {
        path = "/";
    }

    // Rewrite secure
    boolean secure =
            (cookie.isSecure() && request.getOriginalRequest().getRequestLine().getUri().startsWith("https"));

    BasicClientCookie cookieToForward = new BasicClientCookie(name, cookie.getValue());
    if (domain != null) {
        cookieToForward.setDomain(domain);
    }
    cookieToForward.setPath(path);
    cookieToForward.setSecure(secure);
    cookieToForward.setComment(cookie.getComment());
    cookieToForward.setVersion(cookie.getVersion());
    cookieToForward.setExpiryDate(cookie.getExpiryDate());

    if (((BasicClientCookie) cookie).containsAttribute(CookieUtil.HTTP_ONLY_ATTR)) {
        cookieToForward.setAttribute(CookieUtil.HTTP_ONLY_ATTR, "");
    }

    if (LOG.isDebugEnabled()) {
        // Ensure .toString is only called if debug enabled.
        LOG.debug("Forwarding cookie {} -> {}", cookie.toString(), cookieToForward.toString());
    }
    return cookieToForward;
}
 
Example 20
Source File: ApacheHttpClient.java    From karate with MIT License 4 votes vote down vote up
@Override
protected HttpResponse makeHttpRequest(HttpEntity entity, ScenarioContext context) {
    if (entity != null) {
        requestBuilder.setEntity(entity);
        requestBuilder.setHeader(entity.getContentType());
    }
    HttpUriRequest httpRequest = requestBuilder.build();
    CloseableHttpClient client = clientBuilder.build();
    BasicHttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(URI_CONTEXT_KEY, getRequestUri());
    CloseableHttpResponse httpResponse;
    byte[] bytes;
    try {
        httpResponse = client.execute(httpRequest, httpContext);
        HttpEntity responseEntity = httpResponse.getEntity();
        if (responseEntity == null || responseEntity.getContent() == null) {
            bytes = new byte[0];
        } else {
            InputStream is = responseEntity.getContent();
            bytes = FileUtils.toBytes(is);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HttpRequest actualRequest = context.getPrevRequest();
    HttpResponse response = new HttpResponse(actualRequest.getStartTime(), actualRequest.getEndTime());
    response.setUri(getRequestUri());
    response.setBody(bytes);
    response.setStatus(httpResponse.getStatusLine().getStatusCode());
    for (Cookie c : cookieStore.getCookies()) {
        com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
        cookie.put(DOMAIN, c.getDomain());
        cookie.put(PATH, c.getPath());
        if (c.getExpiryDate() != null) {
            cookie.put(EXPIRES, c.getExpiryDate().getTime() + "");
        }
        cookie.put(PERSISTENT, c.isPersistent() + "");
        cookie.put(SECURE, c.isSecure() + "");
        response.addCookie(cookie);
    }
    cookieStore.clear(); // we rely on the StepDefs for cookie 'persistence'
    for (Header header : httpResponse.getAllHeaders()) {
        response.addHeader(header.getName(), header.getValue());
    }
    return response;
}