* @return the HTTP response or <TT>null</TT> on errors.
*/
private static String send(final EntityEnclosingMethod httpMethod, String url,
RequestEntity requestEntity, String username, String pw) {
HttpClient client = new HttpClient();
HttpConnectionManager connectionManager = client.getHttpConnectionManager();
try {
setAuth(client, url, username, pw);
connectionManager.getParams().setConnectionTimeout(5000);
if (requestEntity != null)
httpMethod.setRequestEntity(requestEntity);
int status = client.executeMethod(httpMethod);
switch (status) {
case HttpURLConnection.HTTP_OK:
case HttpURLConnection.HTTP_CREATED:
case HttpURLConnection.HTTP_ACCEPTED:
String response = IOUtils.toString(httpMethod.getResponseBodyAsStream());
// LOGGER.info("================= POST " + url);
if (LOGGER.isInfoEnabled())
LOGGER.info("HTTP " + httpMethod.getStatusText() + ": " + response);
return response;
default:
LOGGER.warn("Bad response: code[" + status + "]" + " msg[" + httpMethod.getStatusText() + "]"
+ " url[" + url + "]" + " method[" + httpMethod.getClass().getSimpleName()
+ "]: " + IOUtils.toString(httpMethod.getResponseBodyAsStream()));
return null;
}
} catch (ConnectException e) {
LOGGER.info("Couldn't connect to [" + url + "]");
return null;
} catch (IOException e) {
LOGGER.error("Error talking to " + url + " : " + e.getLocalizedMessage());
return null;
} finally {
if (httpMethod != null)
httpMethod.releaseConnection();
connectionManager.closeIdleConnections(0);
}
}