Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Cookies containing equals ("=") sign in Java

I am sending request in the following way.

    HttpClient httpClient=new DefaultHttpClient();
    CookieStore cookieStore=new BasicCookieStore();
    HttpContext httpContext=new BasicHttpContext();
    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    httpClient.execute(httppost,httpContext);

My auth cookie on java side reads like this.

XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwidEFlbVlLYlpuRXYyc29TNjBSOHhueCIsMTM3MzM0NzcyMCwxMzczMzQ3NzIwXX0\075|1373347720|5c1ad3ac3828516aa7178f00b3bba961fa29ae9b

(notice \075)

On server side it is like this.

XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwiejJYUXpQQVhBQ0lQVkdCQU5FMkRtdSIsMTM3MzM0OTIyNiwxMzczMzQ5MjI2XX0

When I use python requests the cookie shown is as follows.

XNlciI6WzU2Mjk0OTk1MzQyMTMxMjAsMCwiclYzYW1FakRHc0dhampDcnhoMlBIVyIsMTM3MzM0OTEzNiwxMzczMzQ5MTM2XX0=|1373349137|e8900c8bfd2972ca4115ef1946b4cdf161a4815a

It appears that HttpClient is ignoring the bit after | (date code and stuff). Am I missing something? I tried all the cookie policies as well, nothing working.

like image 681
specialscope Avatar asked Dec 06 '25 23:12

specialscope


1 Answers

OK I solved it in following way. Before I answer a few facts.

  1. Cookies key-value pair are separated by "=".
  2. Cookies that have "=" inside them must be escaped (the "=" part).
  3. Cookies that include "=" need to be specified as Version1 cookies
  4. Such cookies must be enclosed in double quotes.

This is my entire post method.

    public String doUrlPost(final String connurl,final JSONObject obj) throws IOException{
              CookieManager cookieManager = new CookieManager();
      CookieHandler.setDefault(cookieManager);

      URL url=new URL(connurl);
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setDoOutput(true);
         urlConnection.setRequestMethod("POST");
         urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
                 //sets the cookie to version 1
         urlConnection.setRequestProperty("Cookie2","$Version=1");

         List<HttpCookie> lst=((CookieManager)CookieHandler.getDefault()).getCookieStore().getCookies();
         for(HttpCookie cookie:lst){

             if(cookie.getName().equals("auth")){
             //double quote your cookie
              urlConnection.setRequestProperty("Cookie","auth=\""+cookie.getValue()+"\"");
             }
         }
         urlConnection.setUseCaches(false);      

         List<NameValuePair> nameValuePairs = getData(obj);

         OutputStream out = urlConnection.getOutputStream();
         BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(out, "UTF-8"));           
         writer.write(getQuery(nameValuePairs));
         writer.close();

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        BufferedReader rd=new BufferedReader(new InputStreamReader(in));
        String line="";
        String content="";
        while((line=rd.readLine())!=null){
            content+=line;
        }
        rd.close();
        finalcontent=content;
        urlConnection.disconnect(); 
        return finalcontent;
} 

Get query method

    private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params)
    {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}
like image 176
specialscope Avatar answered Dec 08 '25 14:12

specialscope



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!