Apr 12, 2011

Java to do HTTP Post and read response's content


public void postToCallbackUrl()
{


int retry_count = 0;

while (retry_count < 3)
{
try
{
// do HTTP request
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("print_image_url", printImageUrl));
formparams.add(new BasicNameValuePair("signature_image_url", signatureImageUrl));
formparams.add(new BasicNameValuePair("order_key", orderKey));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");

HttpPost httppost = new HttpPost(callbackUrl);
httppost.setEntity(entity);

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);

logger.debug("Got response:" + IOUtils.toString(response.getEntity().getContent(), "UTF-8"));

break;
}
catch (Exception e) {
retry_count++;

logger.error("Post error",e);
}
}
}