List of usage examples for org.apache.http.impl.client HttpClients createDefault
public static CloseableHttpClient createDefault()
From source file:com.github.brandtg.switchboard.TestMysqlLogServer.java
@BeforeClass public void beforeClass() throws Exception { httpClient = HttpClients.createDefault(); pollMillis = 1000;//from w w w . j a v a 2 s . c o m timeoutMillis = 30000; jdbc = "jdbc:mysql://localhost:3306/test"; serverAddress = new HttpHost("localhost", 8080); adminAddress = new HttpHost("localhost", 8081); }
From source file:net.duckling.ddl.util.RESTClient.java
public JsonObject httpGet(String url, List<NameValuePair> params) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w. j a v a 2s.c om RequestBuilder rb = RequestBuilder.get().setUri(new URI(url)); for (NameValuePair hp : params) { rb.addParameter(hp.getName(), hp.getValue()); } rb.addHeader("accept", "application/json"); HttpUriRequest uriRequest = rb.build(); HttpResponse response = httpclient.execute(uriRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(br); return je.getAsJsonObject(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } finally { httpclient.close(); } return null; }
From source file:Vdisk.java
public JSONObject vdisk_post_operation(URI uri) throws IOException { CloseableHttpClient postClient = HttpClients.createDefault(); if (access_token == null) this.get_access_token(); HttpPost httpPost = new HttpPost(uri); JSONObject res = null;/*from w ww . ja v a 2 s. c om*/ try (CloseableHttpResponse response = postClient.execute(httpPost)) { HttpEntity entity = response.getEntity(); String info = EntityUtils.toString(entity); res = JSONObject.fromObject(info); } finally { postClient.close(); } return res; }
From source file:org.fcrepo.camel.FcrepoHttpClientBuilder.java
/** * Build an HttpClient//from w ww.j a va2s.c om * * @return an HttpClient */ public CloseableHttpClient build() { if (isBlank(username) || isBlank(password)) { return HttpClients.createDefault(); } else { LOGGER.debug("Accessing fcrepo with user credentials"); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope scope = null; if (isBlank(host)) { scope = new AuthScope(AuthScope.ANY); } else { scope = new AuthScope(new HttpHost(host)); } credsProvider.setCredentials(scope, new UsernamePasswordCredentials(username, password)); return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); } }
From source file:sms.SendRequest.java
/** * /*w w w .j a va 2s . com*/ * @param moId * @param sender * @param serviceId * @param receiver * @param contentType * @param messageType * @param username * @param messageIndex * @param message * @param operator * @param commandCode * @return * @throws URISyntaxException * @throws IOException */ public String sendRequests(String moId, String sender, String serviceId, String message, String operator, String commandCode, String username) throws URISyntaxException, IOException { Date d = new Date(); SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); URI uri = new URIBuilder().setScheme("http").setHost("222.255.167.6:8080").setPath("/SMS/Ctnet/sms") .setParameter("mo_id", moId).setParameter("username", username) .setParameter("key", Common.getMD5(username + "|" + moId + "|ctnet2015")) .setParameter("sender", sender).setParameter("serviceId", serviceId) .setParameter("message", message).setParameter("operator", operator) .setParameter("commandCode", commandCode).build(); HttpGet httpPost = new HttpGet(uri); httpPost.addHeader("content-type", "application/x-www-form-urlencoded"); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = httpclient.execute(httpPost); try { HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); if (len != -1 && len < 2048) { return EntityUtils.toString(entity); } else { // Stream content out } } } finally { response.close(); } return ""; }
From source file:org.bireme.cl.CheckUrl.java
public static int check(final String url, final boolean checkOnlyHeader) { if (url == null) { throw new NullPointerException(); }/*from w ww .ja v a2s .c o m*/ final CloseableHttpClient httpclient = HttpClients.createDefault(); int responseCode = -1; try { final HttpRequestBase httpX = checkOnlyHeader ? new HttpHead(fixUrl(url)) : new HttpGet(fixUrl(url)); //final HttpHead httpX = new HttpHead(fixUrl(url)); // Some servers return 500 //final HttpGet httpX = new HttpGet(fixUrl(url)); httpX.setConfig(CONFIG); httpX.setHeader(new BasicHeader("User-Agent", "Wget/1.16.1 (linux-gnu)")); httpX.setHeader(new BasicHeader("Accept", "*/*")); httpX.setHeader(new BasicHeader("Accept-Encoding", "identity")); httpX.setHeader(new BasicHeader("Connection", "Keep-Alive")); // Create a custom response handler final ResponseHandler<Integer> responseHandler = new ResponseHandler<Integer>() { @Override public Integer handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { return response.getStatusLine().getStatusCode(); } }; responseCode = httpclient.execute(httpX, responseHandler); } catch (Exception ex) { responseCode = getExceptionCode(ex); } finally { try { httpclient.close(); } catch (Exception ioe) { System.err.println(ioe.getMessage()); } } return (((responseCode == 403) || (responseCode == 500)) && checkOnlyHeader) ? check(url, false) : responseCode; }
From source file:de.adesso.referencer.search.helper.ElasticConfig.java
public static String sendHttpRequest(String url, String requestBody, String requestType) throws IOException { String result = null;/*from w w w . j av a 2s.c om*/ CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse httpResponse; try { switch (requestType) { case "Get": httpResponse = httpclient.execute(new HttpGet(url)); break; case "Post": HttpPost httppost = new HttpPost(url); if (requestBody != null) httppost.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); httpResponse = httpclient.execute(httppost); break; case "Put": HttpPut httpPut = new HttpPut(url); httpPut.addHeader("Content-Type", "application/json"); httpPut.addHeader("Accept", "application/json"); if (requestBody != null) httpPut.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); httpResponse = httpclient.execute(httpPut); break; case "Delete": httpResponse = httpclient.execute(new HttpDelete(url)); break; default: httpResponse = httpclient.execute(new HttpGet(url)); break; } try { HttpEntity entity1 = httpResponse.getEntity(); if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < MAX_CONTENT_LENGTH) { result = EntityUtils.toString(entity1, DEFAULT_CHARSET); } else { System.out.println("Error!!!! entity length=" + len); } } EntityUtils.consume(entity1); } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { httpclient.close(); } return result; }
From source file:org.mule.service.http.impl.functional.server.HttpServerTransfer10TestCase.java
private void verify10Headers(String path, String expectedConnection, Matcher<Object> contentLengthMatcher, String expectedBody) throws IOException { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(getUri(path)); httpGet.setProtocolVersion(getVersion()); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { assertThat(getHeaderValue(response, CONNECTION), is(expectedConnection)); assertThat(getHeaderValue(response, CONTENT_LENGTH), contentLengthMatcher); assertThat(IOUtils.toString(response.getEntity().getContent()), is(expectedBody)); }/*from ww w .ja v a 2 s . c om*/ } }
From source file:org.elasticsearch.options.detailederrors.DetailedErrorsDisabledIT.java
public void testThatErrorTraceParamReturns400() throws Exception { // Make the HTTP request HttpResponse response = new HttpRequestBuilder(HttpClients.createDefault()) .httpTransport(internalCluster().getDataNodeInstance(HttpServerTransport.class)) .addParam("error_trace", "true").method(HttpDeleteWithEntity.METHOD_NAME).execute(); assertThat(response.getHeaders().get("Content-Type"), is("application/json")); assertThat(response.getBody(), is("{\"error\":\"error traces in responses are disabled.\"}")); assertThat(response.getStatusCode(), is(400)); }