0

I'm writing an Android client that is supposed to communicate with a web application (running on appengine with webapp2). The client sends POST request on the server and needs to authenticate first.

The application stores authentication tokens as session cookies. Those cookies have an equality sign in their value. For example

Set-Cookie: auth=bsoq64sesba=als|sadasd|sdads

I'm using HttpURLConnection on the client side. The problem is that the cookies get truncated if they contain the equality sign. So when the next request is sent, the server receives the following cookie:

auth=bsoq64sesba

Which is useless of course. Is there any way to properly receive cookies with an equality sign in their value?

Here is the code that sends requests

/*The cookie store is initiated once, before the first request*/
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

/*Prepare the connection*/
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setChunkedStreamingMode(0);

/*Write request body*/
OutputStream ostream = connection.getOutputStream();
ostream.write(postBody);
ostream.flush();

/*Read response*/
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseContents = "";
String line;
while ((line=reader.readLine())!=null) responseContents+=line;
return responseContents;
nidzo732
  • 721
  • 1
  • 8
  • 10

1 Answers1

0

You will need to encode the = before you send the cookie value and decode it when you receive it. I would suggest URL encoding it. You can't use Base64 encoding because it uses = in it encoding.

Here is a link to how do this in Java.

Community
  • 1
  • 1
Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
  • 1
    I've already written clients in Python and C# that handle such cookies just fine. Anyway, it turned out that the underlying HTTP library (it looks like Android uses apache httpclient internally) was buggy, the version 4.0 used String.split("=") to separate name and value fields, and it only checked for the first two elements of the returned array. I've managed to get a newer version of the Apache library (4.3) which solves this bug and handles cookies without a problem. – nidzo732 Nov 06 '14 at 21:40