• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps HTTP 1.1 Bad Request while trying to send a cookie with a HttpGet request

rameezh88

Lurker
Aug 17, 2012
2
0
I am developing an Android app for a website. I'm trying to get some information from the website by sending a cookie using a HttpGet request. I don't know where I am going wrong with the request. I am using AsyncTask to send the request to the server.
The request also uses a token which I receive when I login to the webpage. All this is clear in the code below. This is part of the process in the doInBackground() function of the Asynctask:




BufferedReader in = null;

JSONObject token = Login.getToken(); // Token received after login, used in the cookie

try {
r = token.get("session_token").toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

DefaultHttpClient httpclient = (DefaultHttpClient) getHttpClient();


BasicClientCookie ck = new BasicClientCookie("session_token", r);

// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();

// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context

cookieStore.addCookie(ck);

localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet httpget = new HttpGet("http://example.com/api/page");

try {

HttpResponse response = httpclient.execute(httpget, localContext);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuilder builder = new StringBuilder();
String line=null;

for (line = null; (line = in.readLine()) != null;) {
builder.append(line).append("\n");
}


The getHttpClient function looks like:


public static final int HTTP_TIMEOUT = 30 * 1000;

private HttpClient mHttpClient;

private HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}



If somebody could please help me out. It would be great.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones