Java HttpPost no response

348 views
Skip to first unread message

Martin Kondov

unread,
Apr 13, 2012, 11:42:50 AM4/13/12
to FreshBooks API
I am getting an "OK" status,but no XML content in the response.
Using HttpClient API for Java.

HttpPost post= new HttpPost("https://MYLOGIN.freshbooks.com/api/2.1/
xml-in");
System.out.println(post.getRequestLine());
StringEntity se = new StringEntity("<request method=\"system.current
\"></request>","UTF-8");

se.setContentType("text/xml");
post.setHeader("Content-Type","application/x-www-form-urlencoded");
post.setEntity(se);

HttpResponse response = httpclient.execute(post);
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().toString());

HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
System.out.println(instream.toString());
}
}

Output in console:

HTTP/1.1 200 OK
org.apache.http.conn.EofSensorInputStream@17e845a


Please,somebody tell me what I am missing. :(

Martin Kondov

unread,
Apr 13, 2012, 11:44:12 AM4/13/12
to FreshBooks API
Forgot to mention that this code foolows the HttpClient connection and
the connection is established successfully (as the 200 "OK" code
signifies).

Martin Kondov

unread,
Apr 13, 2012, 1:06:41 PM4/13/12
to FreshBooks API
Nevermin,I figured it out.For those who want the solution:

HttpResponse response = httpclient.execute(post);
String content = EntityUtils.toString(entity);
System.out.println(content);

is the proper way to gather the response...

I would appreciate if somebody shed some light on the subject.In
particular,why my method did not work and why do we must use
EntityUtils to parse the entity...isn't the response send as standard
xml?
Reply all
Reply to author
Forward
0 new messages