List of usage examples for org.apache.http.impl.client HttpClientBuilder create
public static HttpClientBuilder create()
From source file:com.whatsthatlight.teamcity.hipchat.HipChatApiProcessor.java
public HipChatEmoticons getEmoticons(int startIndex) { try {//from w w w. j a va2 s. co m URI uri = new URI( String.format("%s%s?start-index=%s", this.configuration.getApiUrl(), "emoticon", startIndex)); String authorisationHeader = String.format("Bearer %s", this.configuration.getApiToken()); // Make request HttpClient client = HttpClientBuilder.create().build(); HttpGet getRequest = new HttpGet(uri.toString()); getRequest.addHeader(HttpHeaders.AUTHORIZATION, authorisationHeader); getRequest.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString()); HttpResponse getResponse = client.execute(getRequest); StatusLine status = getResponse.getStatusLine(); if (status.getStatusCode() != HttpStatus.SC_OK) { logger.error(String.format("Could not retrieve emoticons: %s %s", status.getStatusCode(), status.getReasonPhrase())); return new HipChatEmoticons(new ArrayList<HipChatEmoticon>(), 0, 0, null); } Reader reader = new InputStreamReader(getResponse.getEntity().getContent()); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(reader, HipChatEmoticons.class); } catch (Exception e) { logger.error("Could not get emoticons", e); } return new HipChatEmoticons(new ArrayList<HipChatEmoticon>(), 0, 0, null); }
From source file:org.mitre.openid.connect.service.impl.InMemoryClientLogoLoadingService.java
public InMemoryClientLogoLoadingService() { this(HttpClientBuilder.create().useSystemProperties().build()); }
From source file:org.openiot.integration.VirtualSensor.java
public void createAndRegister() throws IOException, InterruptedException { HttpClient client = HttpClientBuilder.create().build(); String name = "FER" + virtualSensorID; WriteXMLFile xmlFile = new WriteXMLFile(name, virtualSensorPort, sensorParameters, sensorTypes); String virtualSensor = xmlFile.createXML(); String url = "http://" + gsnAddress + "/vs/vsensor/" + name + "/create"; HttpPost request = new HttpPost(url); StringEntity input = new StringEntity(virtualSensor); input.setContentType("text/xml"); request.setEntity(input);/* www. jav a2 s. c o m*/ HttpResponse response = client.execute(request); StatusLine statusLine = response.getStatusLine(); boolean result = statusLine.getStatusCode() == 200; EntityUtils.toString(response.getEntity()); response.getEntity().getContent().close(); WriteMetadataFile metaFile = new WriteMetadataFile(name, virtualSensorID, latitude, longitude, sensorParameters, lsmProperty, lsmUnit); String sensorInstance = metaFile.createMetadata(); url = "http://" + gsnAddress + "/vs/vsensor/" + name + "/register"; request = new HttpPost(url); input = new StringEntity(sensorInstance); input.setContentType("text/xml"); request.setEntity(input); response = client.execute(request); statusLine = response.getStatusLine(); result = statusLine.getStatusCode() == 200; EntityUtils.toString(response.getEntity()); response.getEntity().getContent().close(); }
From source file:com.muhardin.endy.training.ws.aplikasi.absen.rest.client.AbsenRestClient.java
public AbsenRestClient() { try {//from w w w.ja v a 2 s. c o m SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build()); CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("endy", "123"); provider.setCredentials(AuthScope.ANY, credentials); HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider) .setSSLSocketFactory(sslsf).build(); restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client)); restTemplate.setErrorHandler(new AbsenRestClientErrorHandler()); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) { Logger.getLogger(AbsenRestClient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:SearchForSimilarImages.java
public String GetUrlContentAsString(String searchToken, String tagsString, String imageTypeAsString, String sizeAsString, String licenseAsString, String safeSearchAsString) { StringBuffer body = null;//from w w w. ja v a 2 s .c o m String imageType = null, sizeType = null, licenseType = null, safeSearchType = null; try { if (!imageTypeAsString.equals("unspecified")) { imageType = "&imagetype=" + imageTypeAsString; } else { imageType = ""; } if (!sizeAsString.equals("unspecified")) { sizeType = "&size=" + sizeAsString; } else { sizeType = ""; } if (!licenseAsString.equals("unspecified")) { licenseType = "&license=" + licenseAsString; } else { licenseType = ""; } if (!safeSearchAsString.equals("unspecified")) { safeSearchType = "&safesearch=" + safeSearchAsString; } else { safeSearchType = ""; } String count = "&count=2"; // String count = ""; String url = "https://bingapis.azure-api.net/api/v5/images/search?q=" + tagsString + count + "&mkt=en-us" + imageType + sizeType + licenseType + safeSearchType; System.out.println("url to search " + url); HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); request.setHeader("Ocp-Apim-Subscription-Key", searchToken); HttpResponse response = client.execute(request); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); body = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { body.append(line); } // System.out.println(body); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return body.toString(); }
From source file:org.eclipse.microprofile.health.tck.SimpleHttp.java
protected Response getUrlContents(String theUrl, boolean useAuth, boolean followRedirects) { StringBuilder content = new StringBuilder(); int code;// ww w . java 2s .com try { HttpClientBuilder builder = HttpClientBuilder.create(); if (!followRedirects) { builder.disableRedirectHandling(); } if (useAuth) { CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "password"); provider.setCredentials(AuthScope.ANY, credentials); builder.setDefaultCredentialsProvider(provider); } HttpClient client = builder.build(); HttpResponse response = client.execute(new HttpGet(theUrl)); code = response.getStatusLine().getStatusCode(); if (response.getEntity() != null) { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line; while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); } bufferedReader.close(); } } catch (Exception e) { throw new RuntimeException(e); } return new Response(code, content.toString()); }
From source file:DataSci.main.JsonRequestResponseController.java
/** * Call REST API for retrieving prediction from Azure ML * @return response from the REST API/* w ww. ja v a 2 s. co m*/ */ public String rrsHttpPost() { HttpPost post; HttpClient client; StringEntity entity; try { // create HttpPost and HttpClient object post = new HttpPost(apiurl); client = HttpClientBuilder.create().build(); // setup output message by copying JSON body into // apache StringEntity object along with content type entity = new StringEntity(jsonBody/*, HTTP.UTF_8*/); // entity.setContentEncoding(HTTP.UTF_8); entity.setContentType("text/json"); // add HTTP headers post.setHeader("Accept", "text/json"); post.setHeader("Accept-Charset", "UTF-8"); // set Authorization header based on the API key post.setHeader("Authorization", ("Bearer " + apikey)); post.setEntity(entity); // Call REST API and retrieve response content HttpResponse authResponse = client.execute(post); return EntityUtils.toString(authResponse.getEntity()); } catch (Exception e) { return e.toString(); } }
From source file:com.alibaba.dubbo.rpc.protocol.springmvc.support.ApacheHttpClient.java
public ApacheHttpClient() { this(HttpClientBuilder.create().build()); }
From source file:com.kurento.kmf.test.media.MediaApiPlayerNoBrowserTest.java
@Test public void testPlayer() throws Exception { // Media Pipeline MediaPipeline mp = pipelineFactory.create(); PlayerEndpoint playerEP = mp.newPlayerEndpoint("http://files.kurento.org/video/small.webm").build(); HttpGetEndpoint httpEP = mp.newHttpGetEndpoint().terminateOnEOS().build(); playerEP.connect(httpEP);/*from w ww . j av a2 s . c o m*/ playerEP.play(); // Test execution HttpClient client = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(httpEP.getUrl()); HttpResponse response = client.execute(httpGet); HttpEntity resEntity = response.getEntity(); // Assertions Assert.assertEquals("Response content-type must be video/webm", "video/webm", resEntity.getContentType().getValue()); }
From source file:nl.b3p.viewer.ViewerIntegrationTest.java
/** * initialize http client. */ @BeforeClass public static void setUpClass() { client = HttpClientBuilder.create().build(); }