/** * Creates a StringEntityHC4 with the specified content and charset. The MIME type defaults to * "text/plain". * * @param string content to be used. Not {@code null}. * @param charset character set to be used. May be {@code null}, in which case the default is * {@link HTTP#DEFAULT_CONTENT_CHARSET} is assumed * @throws IllegalArgumentException if the string parameter is null * @throws UnsupportedCharsetException Thrown when the named charset is not available in this * instance of the Java virtual machine */ public StringEntityHC4(final String string, final String charset) throws UnsupportedCharsetException { this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset)); }
/** * Creates a StringEntityHC4 with the specified content and charset. The MIME type defaults to * "text/plain". * * @param string content to be used. Not {@code null}. * @param charset character set to be used. May be {@code null}, in which case the default is * {@link HTTP#DEFAULT_CONTENT_CHARSET} is assumed * @throws IllegalArgumentException if the string parameter is null * @since 4.2 */ public StringEntityHC4(final String string, final Charset charset) { this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset)); }
/** @author Corezoid <*****@*****.**> */ public class HttpManager { // ---------------------------------------------------------------------------------------------------------------------- private final HttpClient httpClient; // ---------------------------------------------------------------------------------------------------------------------- /** * create PoolingClientConnectionManager * * @param maxCount max total connection, default max connection per route * @param connectionTimeout milliseconds * @param answerTimeout milliseconds */ public HttpManager(int maxCount, int connectionTimeout, int answerTimeout) { PoolingClientConnectionManager manager = new PoolingClientConnectionManager(); manager.setDefaultMaxPerRoute(maxCount); manager.setMaxTotal(maxCount); HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, answerTimeout); httpClient = new DefaultHttpClient(manager, params); } // ---------------------------------------------------------------------------------------------------------------------- public HttpManager() { this(15, 1000, 10000); } // ---------------------------------------------------------------------------------------------------------------------- /** * send request * * @param message - corezoid message * @return * @throws org.apache.http.HttpException */ public String send(CorezoidMessage message) throws HttpException { HttpPost post = new HttpPost(message.url); post.setEntity(new StringEntity(message.body, jsonUTF8)); return sendBasic(post); } // ---------------------------------------------------------------------------------------------------------------------- private String sendBasic(HttpRequestBase request) throws HttpException { try { HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); String body = ""; if (entity != null) { body = EntityUtils.toString(entity, UTF_8); if (entity.getContentType() == null) { body = new String(body.getBytes(ISO_8859_1), UTF_8); } } int code = response.getStatusLine().getStatusCode(); if (code < 200 || code >= 300) { throw new Exception(String.format(" code : '%s' , body : '%s'", code, body)); } return body; } catch (Exception ex) { throw new HttpException( "Fail to send " + request.getMethod() + " request to url " + request.getURI() + ", " + ex.getMessage(), ex); } finally { request.releaseConnection(); } } // ---------------------------------------------------------------------------------------------------------------------- private static final ContentType jsonUTF8 = ContentType.create("application/json", "UTF-8"); }