0

I have a problem I have been working on for the past few hours and can't seem to figure it out for the life of me.

I have an application that logs the user into my server with some data added to the httppost entity. The first time data is added to the entity everything works fine. The second time any post request is sent to the server, the client hangs until it times out.

My DefaultHttpClient is stored as a global variable in my application and loaded into my class when load_client() is called. Everything works when there are is nothing in the pairs variable, but once a pairs variable is set in setEntity a second time, the program never makes it to the "Response Received" message.

httppost = new HttpPost(format_url(base_url,controller));
logger.info("Sending Post Request");
load_client();
try {
    logger.info("Setting Entity");
    httppost.setEntity(new UrlEncodedFormEntity(pairs,"UTF-8"));
    for(NameValuePair pair : pairs){
        logger.info(pair.getName() + " : " + pair.getValue());
    }
    logger.info("URL Encoded");
    response = httpclient.get_client().execute(httppost);
    logger.info("Received response");

    HttpEntity entity = response.getEntity();
    StatusLine statusLine = response.getStatusLine();

    logger.info("Status: " + statusLine.toString());
    is = entity.getContent();
    logger.info("Content: " + is.toString());

}catch(Exception e){
    logger.error("Error in http connection "+e.toString());
}

Any help would be appreciated. I really have no idea what to do at this point besides saving the old client's cookies and passing a new object to the application every time I want to make a Post request. That seems like a bad way to solve the problem.

EDIT: Here is the message after the client times out. It is never thrown when there is no data in the post request.

Error in http connection org.apache.http.NoHttpResponseException: The target server failed to respond

EDIT: Alright, I have this bug narrowed down to a single line.

DefaultHttpClient defhttpClient = new DefaultHttpClient();
logger.info("URL Encoded");
//When I pass the cookies things stop working...
//defhttpClient.setCookieStore(httpclient.get_cookies());
response = defhttpClient.execute(httppost);
logger.info("Received response");

If I make a new DefaultHttpClient and run the post request with parameters, the client can talk to the server just fine. The only problem is that I need the cookies from the old client to tell the server who the current user is. When I take the cookies from my old client and give them to my new one, the application hangs. I dont even see a request coming into my server. I think I might just have to make a new post for this one...

HarshMarshmallow
  • 1,007
  • 11
  • 23
  • It doesn't make it to response received because *probably* an exception in thrown. Catch it, Log.e it, show the stack trace. – VM4 Aug 14 '14 at 20:33

1 Answers1

0

It seems like there are connection problem. The link below have similar problem so it may be of help: org.apache.http.NoHttpResponseException: The target server failed to respond

Community
  • 1
  • 1
user3806339
  • 144
  • 1
  • 2
  • The connection works if I make the request without any parameters or if I make a call manually. The issue is that I can't add parameters to a request with defaulthttpclient more than once. Is there something that I have to reset every time I set parameters to an entity? The only thing I'm closing now is the inputstream once I've parsed the data. – HarshMarshmallow Aug 14 '14 at 22:44
  • I am not sure about the problem. Can you make the connection if you use the parameters the first time? – user3806339 Aug 15 '14 at 21:48
  • 1
    Yeah. I figured out a little more about what was going wrong. Seems like the cookies stop the defaulthttpclient from using post params after post params are sent through the client the first time. I looked at what was actually in the cookie, and it seems like it's only an s.id with a value attached. Creating a new client allows me to send post params, but the session is lost without the cookies from the old client. I really have no idea. This was working just fine until I migrated servers, then completely stopped working. – HarshMarshmallow Aug 15 '14 at 22:11