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.
Related
I have an android app that connects to a server. The app appends a cookie to each request. However, I need to suppress this cookie for a request to a specific URI.
Some more details:
Can't add an empty cookie for this URI - even if only the empty one is sent, the request must be without a cookie.
Use okHTTP3 for this request. It's ugly but it'll work.
Looked at HttpUrlConnection, CookieStore and CookieManager - could not find any API to disable sending of cookie for a connection
I prefer not to disable cookies in general but just suppress this one
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.
I'm very new to Android programming and I have following doubt.
I use DefaultHttpClient for making Http calls to my server. It works fine.
Are there any way to check whether the session is still live on my DefaultHttpClient object?
Is that possible to do so?
Thanks in advance.
I am working on an app which shall log in to a web site (via http://......?password=xyz).
I use DefaultHttpClient for this.
Along with the GET response, the website sends a cookie, which I want to store for further POST requests.
My problem is that client.getCookieStore().getCookies() always receives an empty list of cookies.
If I open http://www.google.com (insted of my intended website), I receive the cookies properly, but the website I am working with, seems to send the cookie in some other way (it's a MailMan mailing list moderating page)
I can see the respective cookie in Firefox cookie manager, but not in Firebug network/cookie panel (why?). InternetExplorer HttpWatchProfessional however shows the cookie when recording the traffic....
There is some small difference, I observed between the cookies www.google.com sent and my target website: In HttpWatchProfessional, those cookies from google are marked as "Direction: sent", while the cookie from my website are marked as "Direction: Received".
(how can the google cookies be sent, while I cleared browser/cookie cache just before?)
Can someone explain the difference to me?
My code is the following:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse execute = client.execute(httpGet);
List<Cookie> cookies = client.getCookieStore().getCookies();
After further investigation, I found out that the cookie was received, but actually rejected by the httpclient, due to a path the cookie, which differed to that from the called URL.
I found the solution at:
https://stackoverflow.com/a/8280340/1083345
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