List of usage examples for org.apache.http.impl.client HttpClientBuilder create
public static HttpClientBuilder create()
From source file:nl.raja.niruraji.pa.PlayGround.java
public static void main(String[] args) { try {/*from ww w. j a v a 2 s. c o m*/ // create HTTP Client HttpClient httpClient = HttpClientBuilder.create().build(); String url = "http://buienradar.nl/Json/GetTwentyFourHourForecast?geolocationid=2751773"; // Create new getRequest with below mentioned URL HttpGet getRequest = new HttpGet(url); // Add additional header to getRequest which accepts application/xml data //getRequest.addHeader("accept", "application/xml"); // Execute your request and catch response HttpResponse response = httpClient.execute(getRequest); // Check for HTTP response code: 200 = success if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } String result = EntityUtils.toString(response.getEntity()); JSONObject myObject = new JSONObject(result); // Get-Capture Complete application/xml body response BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("============Output:============"); // Simply iterate through XML response and show on console. while ((output = br.readLine()) != null) { System.out.println(output); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ui.pack.MyFrame.java
public static void main(String... args) throws IOException { //MyFrame mf= new MyFrame(); HttpClient client = HttpClientBuilder.create().build(); String url = "http://localhost:8080/students/all"; HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuilder builder = new StringBuilder(); while (true) { String line = bufferedReader.readLine(); if (line == null) { break; } else {/*from ww w .j av a 2 s.c om*/ builder.append(line); } } bufferedReader.close(); String result = builder.toString(); System.out.println(result); JSONArray arr = new JSONArray(result); System.out.println(arr.length()); //client. }
From source file:com.rest.samples.getImagePrintJPanel.java
public static void main(String[] args) { // TODO code application logic here String url = "https://api.adorable.io/avatars/eyes5"; try {/*from w w w . java2 s . co m*/ HttpClient hc = HttpClientBuilder.create().build(); HttpGet getMethod = new HttpGet(url); getMethod.addHeader("accept", "application/png"); HttpResponse res = hc.execute(getMethod); if (res.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode()); } InputStream is = res.getEntity().getContent(); Image image = ImageIO.read(is); JFrame frame = new JFrame(); JLabel label = new JLabel(new ImageIcon(image)); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } catch (IOException ex) { Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.rest.samples.getImage.java
public static void main(String[] args) { // TODO code application logic here String url = "https://api.adorable.io/avatars/eyes1"; try {// www . j ava 2 s .c o m HttpClient hc = HttpClientBuilder.create().build(); HttpGet getMethod = new HttpGet(url); getMethod.addHeader("accept", "application/png"); HttpResponse res = hc.execute(getMethod); if (res.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode()); } InputStream is = res.getEntity().getContent(); OutputStream os = new FileOutputStream(new File("img.png")); int read = 0; byte[] bytes = new byte[2048]; while ((read = is.read(bytes)) != -1) { os.write(bytes, 0, read); } is.close(); os.close(); } catch (IOException ex) { Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.endava.webfundamentals.Main.java
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpRequest = new HttpGet("http://petstore.swagger.wordnik.com/api/api-docs"); HttpResponse httpResponse = httpClient.execute(httpRequest); ObjectMapper objectMapper = new ObjectMapper(); PetStore petStore = objectMapper.readValue(httpResponse.getEntity().getContent(), PetStore.class); PrintWriter out = new PrintWriter("PetStore.html"); out.println("<html>"); out.println("<header>"); out.println(petStore.getInfo().getTitle()); out.println("</header>"); out.println("<body>"); out.println("Api Version " + petStore.getApiVersion()); out.println("Swagger Version " + petStore.getSwaggerVersion()); out.println("<p>"); out.println(petStore.getInfo().getDescription()); out.println("</p>"); out.println("<p>"); out.println(petStore.getInfo().getContact()); out.println("</p>"); out.println(petStore.getInfo().getLicense()); out.println(petStore.getInfo().getLicenseUrl()); out.println("<p>"); out.println(petStore.getInfo().getTermsOfServiceUrl()); out.println("</p>"); out.println("</body>"); out.println("</html>"); out.close();// w w w . j a v a 2s . c o m }
From source file:com.rest.samples.getTipoCambioBanxico.java
public static void main(String[] args) { String url = "http://www.banxico.org.mx/tipcamb/llenarTiposCambioAction.do?idioma=sp"; try {// w w w.ja v a 2 s.com HttpClient hc = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(url); request.setHeader("User-Agent", "Mozilla/5.0"); request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); HttpResponse res = hc.execute(request); if (res.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode()); } BufferedReader rd = new BufferedReader(new InputStreamReader(res.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } Document doc = Jsoup.parse(result.toString()); Element tipoCambioFix = doc.getElementById("FIX_DATO"); System.out.println(tipoCambioFix.text()); } catch (IOException ex) { Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.rest.samples.getReportFromJasperServer.java
public static void main(String[] args) { // TODO code application logic here String url = "http://username:password@10.49.28.3:8081/jasperserver/rest_v2/reports/Reportes/vencimientos.pdf?feini=2016-09-30&fefin=2016-09-30"; try {/*w w w .j a v a2 s .c o m*/ HttpClient hc = HttpClientBuilder.create().build(); HttpGet getMethod = new HttpGet(url); getMethod.addHeader("accept", "application/pdf"); HttpResponse res = hc.execute(getMethod); if (res.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode()); } InputStream is = res.getEntity().getContent(); OutputStream os = new FileOutputStream(new File("vencimientos.pdf")); int read = 0; byte[] bytes = new byte[2048]; while ((read = is.read(bytes)) != -1) { os.write(bytes, 0, read); } is.close(); os.close(); if (Desktop.isDesktopSupported()) { File pdfFile = new File("vencimientos.pdf"); Desktop.getDesktop().open(pdfFile); } } catch (IOException ex) { Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.github.hexsmith.spring.boot.rest.client.ApacheHttpClient.java
public static void main(String[] args) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); HttpClient httpClient = httpClientBuilder.build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(factory); User user = restTemplate.getForObject("http://localhost:8080/json/user", User.class); System.out.println(user);// w w w . j a v a2 s . co m }
From source file:com.rest.samples.GetJSON.java
public static void main(String[] args) { // TODO code application logic here // String url = "https://api.adorable.io/avatars/list"; String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5"; try {// ww w . j a v a 2s.co m HttpClient hc = HttpClientBuilder.create().build(); HttpGet getMethod = new HttpGet(url); getMethod.addHeader("accept", "application/json"); HttpResponse res = hc.execute(getMethod); if (res.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode()); } InputStream is = res.getEntity().getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); JsonParser parser = new JsonParser(); JsonElement element = parser.parse(br); if (element.isJsonObject()) { JsonObject jsonObject = element.getAsJsonObject(); Set<Map.Entry<String, JsonElement>> jsonEntrySet = jsonObject.entrySet(); for (Map.Entry<String, JsonElement> entry : jsonEntrySet) { if (entry.getValue().isJsonArray() && entry.getValue().getAsJsonArray().size() > 0) { JsonArray jsonArray = entry.getValue().getAsJsonArray(); Set<Map.Entry<String, JsonElement>> internalJsonEntrySet = jsonArray.get(0) .getAsJsonObject().entrySet(); for (Map.Entry<String, JsonElement> entrie : internalJsonEntrySet) { System.out.println("---> " + entrie.getKey() + " --> " + entrie.getValue()); } } else { System.out.println(entry.getKey() + " --> " + entry.getValue()); } } } String output; while ((output = br.readLine()) != null) { System.out.println(output); } } catch (IOException ex) { Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:sadl.run.moe.MoeTest2.java
public static void main(String[] args) throws ClientProtocolException, IOException { final String postUrl = "http://pc-kbpool-8.cs.upb.de:6543/gp/next_points/epi";// put in your url try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { // Use this instead final HistoryData h = new HistoryData(); final Configuration c = new Configuration(); final PdttaParameters parameters = new PdttaParameters(); for (final Parameter p : parameters.parameters) { c.config.put(p, p.getDefault()); }//from ww w . j a v a2s. co m h.history.put(c, 0.5); final HttpPost post = new HttpPost(postUrl); final String s = parameters.toJsonString(20, h); // final String s = Files.readAllLines(Paths.get("testfile")).get(0); // final StringEntity postingString = new StringEntity(gson.toJson(p));// convert your pojo to json final StringEntity postingString = new StringEntity(s);// convert your pojo to json post.setEntity(postingString); System.out.println(EntityUtils.toString(post.getEntity(), "UTF-8")); post.setHeader("Content-type", "application/json"); try (CloseableHttpResponse response = httpClient.execute(post)) { final String responseString = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println(responseString); } } // This is the right query // {"domain_info": {"dim": 2, "domain_bounds": [{"max": 1.0, "min": 0.0},{"max": 0.0, "min": -1.0}]}, "gp_historical_info": {"points_sampled": // [{"value_var": 0.01, "value": 0.1, "point": [0.0,0.0]}, {"value_var": 0.01, "value": 0.2, "point": [1.0,-1.0]}]}, "num_to_sample": 1} }