views:

715

answers:

2

Hello experts!

I'm building an android app which should perform a GET on my site to get two cookies and then perform a post to the same site with these cookies.

As mentioned I start of with the GET and I'm using org.apache.http.client.HttpClient to perform this operation.

String requiredCookies = "";
HttpContext localContext = null;

System.out.println("------------------GET----------------------");
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("www.mysitegeturl.com");

//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();

// Creating a local HTTP context
localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);

HttpResponse response;
try {
    response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
   System.out.println("Response content length: " + entity.getContentLength());
}

//Do this so that Java.net impl should work
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
     requiredCookies += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}

if (entity != null) {
    entity.consumeContent();
}

} catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
 e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
System.out.println("------------------GET-END---------------------");

So far so good. Don't mind the requiredCookies line yet, it will be used in the Java.net impl since I can't get the HttpClient one to work =(. Let's take a look at the non working HttpClient Post part.

System.out.println("------------------HttpClient - POST----------------------");
HttpPost post = new HttpPost("www.mysiteposturl.com");

//Params       
HttpParams params = new BasicHttpParams();

params.setParameter("foo", "post");
params.setParameter("bar", "90");
params.setParameter("action", "search");

post.setParams(params);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");

try {
    HttpResponse response2 = httpClient.execute(post, localContext);
     System.out.println(response2.getStatusLine());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
System.out.println("------------------POST END---------------------");

What happens now is that I perform a POST with the localContext where the cookies are stored. This doesn't work. I get a HTTP/1.1 401 No session. Since I had no luck with this I tried another approach(java.net.HttpURLConnection). Remember I still use the same GET part

URL url = new URL("www.mysiteposturl");
HttpURLConnection connection = null;

String dataString = "bar=90&foo=post&action=search";

try {
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie", requiredCookies);
//Set to POST
connection.setDoOutput(true);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(dataString);
writer.flush();
writer.close();
connection.connect();

if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
    System.out.println(connection.getContent().toString());
} else {
   System.out.println("Error");
}
} catch (IOException e) {
   // TODO Auto-generated catch block
     e.printStackTrace();
} catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

System.out.println("------------------POST END---------------------");

And VIOLA a 200 is displayed and everything works like a charm. What do you guys think? Could someone please provide me with an answer because I can't figure it out.

A: 

The problem appears to be that you have two different host names in the setup. This will cause HTTP Client to not send cookies for a different host. You could try changing the domain of the cookies in the cookie store, or using the same host for GET and POST. Additionally you could manually add the cookies to the headers in HTTP Client as you did in the HttpURLConnection example.

Aaron
Yeah that was the problem. To solve it I change the domain for the cookies. Thanks.
jakob
A: 
Christopher