Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Why HttpClient doesn't send my cookies?

I try to send cookies with a form post using the Apache HttpClient and, for some reason, the server gets the request but not the cookies. Here is my code:

            DefaultHttpClient client = new DefaultHttpClient();

            // Set the cookies...
            {
                String Domain = MyGetParameter("Domain");
                BasicCookieStore cookieStore = new BasicCookieStore(); 
                String[] strs = GetParameterSplitted("PostCookies");
                int size = strs.length;
                for (int i=0; i<size-1; i+=2)
                {
                    //JOptionPane.showMessageDialog(null, strs[i]+" = "+FromBase64(strs[i+1], "UTF-8"));
                    BasicClientCookie cookie = new BasicClientCookie(strs[i], FromBase64(strs[i+1], "UTF-8"));
                    cookie.setDomain(Domain);
                    cookie.setPath("/");
                    //cookie.setSecure(true);
                    cookieStore.addCookie(cookie);
                }
                client.setCookieStore(cookieStore);
            }

            HttpPost post = new HttpPost(url.toURI());
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(10);
            // Set the form POST parameters...
            {
                String[] strs = GetParameterSplitted("PostParams");
                int size = strs.length;
                for(int i=0; i<size-1; i+=2)
                {
                    String name = strs[i].trim();
                    String value = FromBase64(strs[i+1].trim(), "UTF-8");//, "UTF-8"

                    nameValuePairs.add(new BasicNameValuePair(name, value));
                }
            }
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            post.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            post.getParams().setParameter(ClientPNames.COOKIE_POLICY, org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);

            HttpResponse response = client.execute(post);
            int StatusCode = response.getStatusLine().getStatusCode();

The site uses HTTP (not HTTPS), I make sure the domain name is set correctly to the cookies (http://mysite) and the cookies seem to be set correctly when the above code executes.

Does anyone have any idea why it's failing to pass them to the server? I have seen other similar questions on this site but nothing seemed to help.

like image 826
user2173353 Avatar asked Dec 03 '22 23:12

user2173353


1 Answers

You look closely, if the date of your cookie expired httptClient not send this cookie, on this that you should put the cookies date.

And in domain name will be no "http://", only simply domain name.

For Example:(http://www.gmail.com => like this to write setDomain("www.gmail.com"))

This example i add 100 day to current day and set cookie. Example send post data via HttpClient with cookie:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 100);
Date date = calendar.getTime();

DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.setCookieStore(new BasicCookieStore());
BasicClientCookie cookie = new BasicClientCookie(YourCookieName, YourCookieValue);
cookie.setDomain(YourDomain);
cookie.setExpiryDate(date);
cookie.setPath("/");
httpClient.getCookieStore().addCookie(cookie);

....

httpClient.execute(yourHttpUriRequest);
like image 181
SBotirov Avatar answered Dec 21 '22 03:12

SBotirov