Skip sending cookies in request, but maintain cookies - android

Is there a simple way to make sure the HttpClient does not send the cookies during a request, without removing all cookies.
By doing:
httpClient.getCookieStore().clear();
cookies are not sent, which is good.
But for other requests (where I need those cookies), I don't want to fetch new cookies again.

You can try to set the Cookie header of the request manually for that particular request
request.setHeader("Cookie","");
Edited:
So if is that not working an other approach could be to try to add an empty cookie store to that HttpClient instance, so theoretically you will have an empty cookiestore
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Add the created cookieStore to the http client instance
httpClient.setCookieStore(cookieStore);

My response is based on assumption that you are using Apache HttpClient version 4.x.
By default HttpClient 4.x introduces two protocol interceptors to the protocol processing pipeline responsible for HTTP state management: RequestAddCookies and ResponseProcessCookies. What you want is to remove RequestAddCookies while leaving ResponseProcessCookies in place.
This is how this can be done with HttpClient 4.3
CloseableHttpClient httpclient = HttpClients.custom()
.disableCookieManagement()
.addInterceptorFirst(new ResponseProcessCookies())
.build();
There are other ways to achieve the same result such as using a custom protocol interceptor that removes cookie headers from all outgoing requests, but removal of RequestAddCookies is the most efficient.

Related

How to get and post cookies through loopj network library in android?

I have an app which requires to save and send cookies back to server but i don't know how to send and get cookies.
Any good soul please help.
I can even accept answers of volley library to get and post cookies.
First, create an instance of AsyncHttpClient:
AsyncHttpClient myClient = new AsyncHttpClient();
Now set this client’s cookie store to be a new instance of PersistentCookieStore, constructed with an activity or application context (usually this will suffice):
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);
Any cookies received from servers will now be stored in the persistent cookie store.
To add your own cookies to the store, simply construct a new cookie and call addCookie:
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome");
newCookie.setVersion(1);
newCookie.setDomain("mydomain.com");
newCookie.setPath("/");
myCookieStore.addCookie(newCookie);
See the PersistentCookieStore Javadoc for more information.

Accept Cookie from domain

I am trying to connect to foodEssentials API and it sends a cookie.
I am getting the error of Cookie rejected: "BasicClientCookie ..."
How do I deal with cookies coming in or is there any examples as I have looked around and found this loopj
I am not sure what I should do. Does the browser handle the cookie or the application?
Browsers handle server side cookies by themselves.
In case of Android application, developer need to explicitly handle the cookies.
Here is an example of handling cookies : http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java
You can use the CookieStore class of Apache http client. Somewhere in your code initialization, create a cookie store and http context:
BasicCookieStore cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
The cookie store will manage all the cookies as long as httpContext is alive.
So later, when doing a request, you just give the context to the httpClient:
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(httpAgent);
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String json = httpClient.execute(request, responseHandler, httpContext);
... and the cookies will be properly managed.
See Android Http Client and Basic Cookie Store.

disable/ignore cookies with httpclient

How can I disable/ignore default cookie handling of httpclient. I want to do it manually. I want to set a pre-defined cookie header for all http requests.
The latest httpclient (4.5.1) has a method called "disableCookieManagement", and it appears this just disables the internal cookie management, not the ability to send or receive cookies, and is working for me-
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#disableCookieManagement()
Set httpclient.setCookieStore(cookieStore); before you execute your HttpClient
Force to setup the HttpContext before executing the request:
private HttpClientContext httpContext = new HttpClientContext();
httpContext.setCookieStore(new BasicCookieStore());
The setCookieStore will create an empty cookie store to replace the default cookie store of http connection.
After that we can execute the http method:
org.apache.http.client.HttpClient.execute(HttpUriRequest, HttpContext)
Also we can reuse the cookie store (hold the BasicCookieStore) to keep the connection alive.
I could not find a way to disable/ignore cookie handling by DefaultHttpClient in Andoroid(I should have explored more into Android source code but have a time limitation). But I resolved it by removing all cookies before doing httpClient.execute() like this -
((AbstractHttpClient) myDefaultHttpClient).getCookieStore().clear();
This removes all the cookies stored by the defaultHttpClient and then you can manually handle(add/delete) cookies using -
myHttpPost.setHeader("Cookie", myCookie);
Hope it helps.

android-spring resttemplate manage cookies

I am using Spring for Android in a project and I need to manage the cookie store/manager. I am able to add cookies to any request by using an implementation of ClientHttpRequestInterceptor, but I would like to remove some of these when sending a request.
To be more specific, the problem I am facing is that, for Froyo, the implementation specific in Spring (with DefaultHttpClient) adds automatically to headers the cookies from CookieStore - that even if I am setting explicitly the headers. But I would like to manage these cookies myself (either remove some of them, or update their values). While for Gingerbread above (Spring implementation is done with HttpURLConnection) the cookies are added only if I am doing it myself - however I am not that sure as I don't see Spring setting any CookieHandler, but the bottom line is that I don't see them when performing a request or I can see them updated. So the issue is more specific to Froyo.
The work-around is to reset the connection factory; something like:
protected void resetCookieStoreForTemplate(RestTemplate template) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) {
template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
}
}
Underneath, that seems to recreate the DefaultHttpClient and will use a new CookieStore. But that seems to me a bit ugly.
To wrap up, my question would be: does Spring for Android provide some method to expose some API for Cookie management? Just the way RestTemplate exposes some abstractions for connectivity, connection factory, message converters and so on, I would be very happy to have some abstraction for cookie management.
I've not used Spring myself but from what I've read about it, it follows the official advice and switches HTTP clients based on API version (which is quite clever, if seriously over-engineered for my liking). When using HTTPUrlConnection, as you mentioned, Spring probably doesn't change the CookieHandler. You should be seeing in-memory cookie handling, so everything should work for requests in the same app run but the cookies would be wiped when you close the app. Can you confirm this is what you're seeing?
If so, all you have to do is create a new CookieManager instance, passing it a custom CookieStore and null for the CookiePolicy to use the default.
It's unfortunate that a persistent store isn't built-in but it's not particularly difficult to write one either.
Edit: See here for a CookieStore that uses SharedPreferences (haven't tested it myself).
The ClientHttpRequestInterceptor class is a good approach when you need to pass common headers for all requests such as setting up content type, authorization etc. As far as I understood you want to pass some cookie values for specific request.
You could also achieve this via HttpEntity and HttpHeaders class.
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "name=" + value);
HttpEntity requestEntity = new HttpEntity(null, requestHeaders);
ResponseEntity response = restTemplate.exchange(
"http://server/service?...",
HttpMethod.GET,
requestEntity,
Response.class);
Spring rest template does not provide any off the self solution for managing cookies. The class CookieHandler is provided by Apache not part of spring. Rest template is just a basic solution for managing request response with compare to spring core.

android http request ignore cookie

I am using DefaultHttpClient and want to prevent HttpClient from accepting and sending cookies. I tried something like this but 'CookiePolicy.IGNORE_COOKIE' is not available.
mHttpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.IGNORE_COOKIE);
You can't just make up your own int Constants lol,
always check the API: http://developer.android.com/reference/java/net/CookiePolicy.html
How about
CookiePolicy.ACCEPT_NONE
http://developer.android.com/reference/java/net/CookiePolicy.html#ACCEPT_NONE

Categories

Resources