Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

How to read response from HttpClient

Hi,

I have the following code which makes a POST - is the way I'm reading the response correctly? I just want to get the text output the server is sending back. This is the snippet of interest:

  StringBuilder sbResponse = new StringBuilder(256);
  DataInputStream inStream = new DataInputStream(response.getEntity().getContent());
  String line;    
  while ((line = inStream.readLine()) != null) {
      sbResponse.append(line);
   }
   inStream.close();  
   String response = sbResponse.toString();

the full code is attached below. Any other considerations I need to make with this?

Thanks

public void postData() 
{
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        StringBuilder sbResponse = new StringBuilder(256);
        DataInputStream inStream = new DataInputStream(response.getEntity().getContent()); 
        String line;     
        while ((line = inStream.readLine()) != null) { 
            sbResponse.append(line);
        } 
        inStream.close(); 

        // All done.
        String response = sbResponse.toString(); 
        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Hi CEHJ,

What do I call getResponseBodyAsString() on though - the HttpResponse and HttpPost objects don't seem to expose that method?

Is the above wrong, will it not work? Or will it work, just that it could be faster?

Thanks
Have you tried your code?  Do you get your response back from the server?

I don't see much improvement in the link provided by CEHJ above, but there are many things about http interaction that you are not doing, such as handling cookies and error conditions.  Otherwise, your basic flow seems right -- make a connection, get the response and read it.  Reading the response line by line should be fine.

But trying to run the code is the best way to tell if it's not doing what you want.  Then, if you can't figure out how to get what you want, you can post the problem here.
Yes, that is how you should read the response in http-client 4.x.

getResponseBodyAsString( ) api is available in 3.x



getContent( ) will only give the response body.

If you want to read any other headers in the http response you get.. you have to use..  other api's avaible in HttpEntity

 [ response.getEnttity( ) will give the instance of HttpEntity]
:-)

That is the optimal way if you need a String. Otherwise, you can look at things like


http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpMethod.html#getResponseBodyAsStream()