List of usage examples for org.apache.http.client.methods HttpPost setEntity
public void setEntity(final HttpEntity entity)
From source file:de.bytefish.fcmjava.client.utils.HttpUtils.java
private static <TRequestMessage> void internalPost(HttpClientBuilder httpClientBuilder, IFcmClientSettings settings, TRequestMessage requestMessage) throws Exception { try (CloseableHttpClient client = httpClientBuilder.build()) { // Initialize a new post Request: HttpPost httpPost = new HttpPost(settings.getFcmUrl()); // Set the JSON String as data: httpPost.setEntity(new StringEntity(JsonUtils.getAsJsonString(requestMessage))); // Execute the Request: try (CloseableHttpResponse response = client.execute(httpPost)) { // Get the HttpEntity: HttpEntity entity = response.getEntity(); // Let's be a good citizen and consume the HttpEntity: if (entity != null) { // Make Sure it is fully consumed: EntityUtils.consume(entity); }/* w w w. ja v a 2 s .c o m*/ } } }
From source file:net.ccghe.utils.Server.java
public static JSONObject Send(PostDataPairs pairs) { HttpClient client = new DefaultHttpClient(); try {/*from w w w. ja v a2s . co m*/ HttpPost post = new HttpPost(serverURL); post.setEntity(new UrlEncodedFormEntity(pairs.get())); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); String jsonResponse = convertStreamToString(stream); stream.close(); if (entity != null) { entity.consumeContent(); } JSONObject jObject = new JSONObject(jsonResponse); return jObject; } catch (ClientProtocolException e) { Log.e("EMOCHA", "ClientProtocolException ERR. " + e.getMessage()); } catch (UnknownHostException e) { Log.e("EMOCHA", "UnknownHostException ERR. " + e.getMessage()); } catch (IOException e) { Log.e("EMOCHA", "IOException ERR. " + e.getMessage()); } catch (Exception e) { Log.e("EMOCHA", "Exception ERR. " + e.getMessage()); } return null; }
From source file:org.fcrepo.auth.roles.common.AbstractRolesIT.java
protected static HttpPost postDSMethod(final String objectPath, final String ds, final String content) throws UnsupportedEncodingException { final HttpPost post = new HttpPost(serverAddress + objectPath + "/" + ds + "/fcr:content"); post.setEntity(new StringEntity(content)); return post;/*w w w.j a v a2 s . c o m*/ }
From source file:com.aurel.track.license.LicenseHelperBL.java
public static String sendPOSTReq(List<BasicNameValuePair> params, String licenseProviderAddress) { String responseStr = ""; try {/* w ww. j av a2 s.c o m*/ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(licenseProviderAddress); // Request parameters and other properties. httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); //Execute and get the response. HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); responseStr = StringArrayParameterUtils.getStringFromInputStream(instream); try { // do something useful } finally { instream.close(); } } } catch (Exception ex) { LOGGER.error(ExceptionUtils.getStackTrace(ex)); } int idx = responseStr.indexOf("}<!DOCTYPE HTML", 0); if (idx > 0) { responseStr = responseStr.substring(0, idx + 1); } return responseStr; }
From source file:com.intel.cosbench.controller.tasklet.AbstractHttpTasklet.java
private static HttpPost prepareRequest(String content, String url) { HttpPost POST = new HttpPost(url); try {/* w w w .java2s .c o m*/ if (StringUtils.isNotEmpty(content)) POST.setEntity(new StringEntity(content)); } catch (Exception e) { throw new UnexpectedException(e); // will not happen } if (content != null && content.length() > 0) if (!content.startsWith("<?xml")) LOGGER.debug("[ >> ] - {} -> {}", content, url); else LOGGER.debug("[ >> ] - [xml-content] -> {}", url); else LOGGER.debug("[ >> ] - [empty-body] -> {}", url); return POST; // HTTP request prepared }
From source file:com.android.tools.idea.stats.UsageTrackerAnalyticsImpl.java
/** * Send a ping to Analytics on a separate thread *//*from w w w. j a va 2 s.c o m*/ private static void sendPing(@NotNull final List<? extends NameValuePair> postData) { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { @Override public void run() { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost request = new HttpPost(ANALYTICS_URL); try { request.setEntity(new UrlEncodedFormEntity(postData)); HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); HttpEntity entity = response.getEntity(); // throw it away, don't care, not sure if we need to read in the response? if (status.getStatusCode() >= 300) { LOG.debug("Non 200 status code : " + status.getStatusCode() + " - " + status.getReasonPhrase()); // something went wrong, fail quietly, we probably have to diagnose analytics errors on our side // usually analytics accepts ANY request and always returns 200 } } catch (IOException e) { LOG.debug("IOException during Analytics Ping", e.getMessage()); // something went wrong, fail quietly } finally { HttpClientUtils.closeQuietly(client); } } }); }
From source file:de.xwic.appkit.core.remote.client.URemoteAccessClient.java
/** * @param param/*w w w. j av a2 s .c om*/ * @param config * @return */ public static byte[] postRequest(final IRequestHelper helper, final RemoteSystemConfiguration config) { CloseableHttpResponse response = null; try { CloseableHttpClient client = PoolingHttpConnectionManager.getInstance().getClientInstance(config); HttpPost post = new HttpPost(helper.getTargetUrl()); post.setEntity(helper.getHttpEntity()); response = client.execute(post); int responseCode = response.getStatusLine().getStatusCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new RemoteDataAccessException(response.getStatusLine().getReasonPhrase()); } return EntityUtils.toByteArray(response.getEntity()); } catch (RemoteDataAccessException re) { throw re; } catch (Exception e) { throw new RemoteDataAccessException(e); } finally { if (response != null) { try { response.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } } }
From source file:com.yahoo.wiki.webservice.application.WikiMain.java
/** * Makes the dimensions passthrough./* w ww. j a v a2 s . co m*/ * <p> * This method sends a lastUpdated date to each dimension in the dimension cache, allowing the health checks * to pass without having to set up a proper dimension loader. For each dimension, d, the following query is * sent to the /v1/cache/dimensions/d endpoint: * { * "name": "d", * "lastUpdated": "2016-01-01" * } * * @param port The port through which we access the webservice * * @throws IOException If something goes terribly wrong when building the JSON or sending it */ private static void markDimensionCacheHealthy(int port) throws IOException { for (DimensionConfig dimensionConfig : new WikiDimensions().getAllDimensionConfigurations()) { String dimension = dimensionConfig.getApiName(); HttpPost post = new HttpPost("http://localhost:" + port + "/v1/cache/dimensions/" + dimension); post.setHeader("Content-type", "application/json"); post.setEntity(new StringEntity( String.format("{\n \"name\":\"%s\",\n \"lastUpdated\":\"2016-01-01\"\n}", dimension))); CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response = client.execute(post); LOG.debug("Mark Dimension Cache Updated Response: ", response); } }
From source file:com.fengduo.bee.commons.util.HttpClientUtils.java
public static String postRequest(String url, List<NameValuePair> postParams) throws Exception { HttpPost post = new HttpPost(url); UrlEncodedFormEntity uefEntity;//from w w w . j a v a 2 s . co m String result = null; try { uefEntity = new UrlEncodedFormEntity(postParams, CHARSET); post.setEntity(uefEntity); HttpResponse rep = client.execute(post); if (rep.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = rep.getEntity(); if (httpEntity != null) { result = EntityUtils.toString(httpEntity, "UTF-8"); } } } catch (Exception e) { throw e; } finally { post.abort(); } return result; }
From source file:com.jimkont.contactFix.provider.ProviderHTTPPost.java
/** * Connects to the server/*from w ww . java2s .c o m*/ */ public static String executePost(String url, ArrayList<NameValuePair> params) { final ResponseHandler<String> responseHandler; HttpEntity entity = null; try { entity = new UrlEncodedFormEntity(params); } catch (final UnsupportedEncodingException e) { // this should never happen. throw new AssertionError(e); } final HttpPost post = new HttpPost(url); post.addHeader(entity.getContentType()); post.setEntity(entity); maybeCreateHttpClient(); try { responseHandler = new BasicResponseHandler(); String responseBody = mHttpClient.execute(post, responseHandler); return responseBody; } catch (final IOException e) { return null; } finally { } }