hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: help with httppost response
Date Wed, 13 Jan 2010 18:23:04 GMT
Ajay Bansal wrote:
> So I checked few thing further.
> 
> Problem is only when there is an error generated from the API. For positive,
> it works fine.
> 
> 		InputStreamEntity reqEntity = null;
> 		try {
> 			reqEntity = new InputStreamEntity(new
> FileInputStream(file), -1);
> 		} catch (FileNotFoundException e1) {
> 			// TODO Auto-generated catch block
> 			e1.printStackTrace();
> 		}
> 		if (reqEntity != null) {
> 			reqEntity.setContentType("application/xml");
> 			reqEntity.setChunked(true);
> 		}
> 		httppost.setEntity(reqEntity);
> 
> 		System.out.println("executing request " +
> httppost.getRequestLine());
> 		ResponseHandler<String> responseHandler = new
> BasicResponseHandler();
> 		String responseBody = null;
> 		try {
> 			responseBody = httpclient.execute(httppost,
> responseHandler);
> 		} catch (org.apache.http.client.HttpResponseException e) {
> 			e.printStackTrace();
> 		} catch (Exception e) {
> 			e.printStackTrace();
> 		}
> 
> When we get an exception, I would like to see the response XML in that case
> as well.
> 
> What can I do?
> 
> Regards
> Ajay
> 
> 
> 
> -----Original Message-----
> From: Ajay Bansal [mailto:ajay.bansal02@gmail.com] 
> Sent: Wednesday, January 13, 2010 4:26 PM
> To: 'HttpClient User Discussion'
> Subject: help with httppost response
> 
> Hi All,
> 
> I am using 4x APIs. Here is my code
> 
> =========================
> 
> 		String baseServer = "http://localhost:8080";
> 
> 		String serverUrl = baseServer + Some API URL;
> 		String signature = genSig.getSignature();
> 
> 		HttpClient httpclient = new DefaultHttpClient();
> 
> 		HttpPost httppost = new HttpPost(serverUrl);
> 		httppost.setHeader(Sets the header);
> 
> 		File file = new File("data/userRegister.xml");
> 
> 		InputStreamEntity reqEntity = new InputStreamEntity(
> 				new FileInputStream(file), -1);
> 		reqEntity.setContentType("application/xml");
> 		reqEntity.setChunked(true);
> 
> 		httppost.setEntity(reqEntity);
> 
> 		System.out.println("executing request " +
> httppost.getRequestLine());
> 		HttpResponse response = httpclient.execute(httppost); /*
> THIS CODE WORKS FINE - I see the REQUEST in the backend */
> 
> /* Problem is I am not able to capture the response using following code.
> What do I need to do? */
> 
> 
> 		HttpEntity resEntity = response.getEntity();
> 
> 		
> 		System.out.println("Status : " + response.getStatusLine());
> 		
> 		if (resEntity != null) {
> 			System.out.println("Response content length: "
> 					+ resEntity.getContentLength());
> /* I always get this as -1*/
> 			System.out.println("Chunked?: " +
> resEntity.isChunked());
> 		}
> 
> 		if (resEntity != null) {
> 			resEntity.consumeContent();
> 		}
>     
> ===============================
> 
> I need to print the response that I get from the above method. How do I do
> that?
> 

http://hc.apache.org/httpcomponents-client/tutorial/html/fundamentals.html#d4e152

Oleg

> Regards
> Ajay
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message