List of usage examples for org.apache.http.client.fluent Request Get
public static Request Get(final String uri)
From source file:com.enitalk.controllers.paypal.Usd.java
public static void main(String[] args) throws IOException, ParseException { String rs = Request.Get("http://www.cbr.ru/scripts/XML_daily.asp") .addHeader("Content-type", "application/xml;charset=utf-8").execute().returnContent() .asString(Charset.forName("windows-1251")); Pair<AutoPilot, VTDNav> bb = getNavigator(rs.getBytes()); String change = getChange(bb.getLeft(), bb.getRight()); System.out.println("Rs " + change); DecimalFormat df = new DecimalFormat(); DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); df.setDecimalFormatSymbols(symbols); BigDecimal dd = BigDecimal.valueOf(df.parse(change).doubleValue()).setScale(2, RoundingMode.CEILING); System.out.println("Dd " + dd); }
From source file:interoperabilite.webservice.fluent.FluentQuickStart.java
public static void main(String[] args) throws Exception { // The fluent API relieves the user from having to deal with manual // deallocation of system resources at the cost of having to buffer // response content in memory in some cases. Request.Get("http://targethost/homepage").execute().returnContent(); Request.Post("http://targethost/login") .bodyForm(Form.form().add("username", "vip").add("password", "secret").build()).execute() .returnContent();// w w w.j av a 2 s.c o m }
From source file:interoperabilite.webservice.fluent.FluentRequests.java
public static void main(String[] args) throws Exception { // Execute a GET with timeout settings and return response content as String. Request.Get("http://somehost/").connectTimeout(1000).socketTimeout(1000).execute().returnContent() .asString();//from w w w .j av a 2 s . com // Execute a POST with the 'expect-continue' handshake, using HTTP/1.1, // containing a request body as String and return response content as byte array. Request.Post("http://somehost/do-stuff").useExpectContinue().version(HttpVersion.HTTP_1_1) .bodyString("Important stuff", ContentType.DEFAULT_TEXT).execute().returnContent().asBytes(); // Execute a POST with a custom header through the proxy containing a request body // as an HTML form and save the result to the file Request.Post("http://somehost/some-form").addHeader("X-Custom-header", "stuff") .viaProxy(new HttpHost("myproxy", 8080)) .bodyForm(Form.form().add("username", "vip").add("password", "secret").build()).execute() .saveContent(new File("result.dump")); }
From source file:org.apache.infra.reviewboard.ReviewBoard.java
public static void main(String... args) throws IOException { URL url = new URL(REVIEW_BOARD_URL); HttpHost host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); Executor executor = Executor.newInstance().auth(host, REVIEW_BOARD_USERNAME, REVIEW_BOARD_PASSWORD) .authPreemptive(host);//from w w w . j a v a 2 s . com Request request = Request.Get(REVIEW_BOARD_URL + "/api/review-requests/"); Response response = executor.execute(request); request = Request.Get(REVIEW_BOARD_URL + "/api/review-requests/"); response = executor.execute(request); ObjectMapper mapper = new ObjectMapper(); JsonNode json = mapper.readTree(response.returnResponse().getEntity().getContent()); JsonFactory factory = new JsonFactory(); JsonGenerator generator = factory.createGenerator(new PrintWriter(System.out)); generator.setPrettyPrinter(new DefaultPrettyPrinter()); mapper.writeTree(generator, json); }
From source file:interoperabilite.webservice.fluent.FluentAsync.java
public static void main(String[] args) throws Exception { // Use pool of two threads ExecutorService threadpool = Executors.newFixedThreadPool(2); Async async = Async.newInstance().use(threadpool); Request[] requests = new Request[] { Request.Get("http://www.google.com/"), Request.Get("http://www.yahoo.com/"), Request.Get("http://www.apache.com/"), Request.Get("http://www.apple.com/") }; Queue<Future<Content>> queue = new LinkedList<Future<Content>>(); // Execute requests asynchronously for (final Request request : requests) { Future<Content> future = async.execute(request, new FutureCallback<Content>() { @Override/*from w w w. j a v a 2 s .c o m*/ public void failed(final Exception ex) { System.out.println(ex.getMessage() + ": " + request); } @Override public void completed(final Content content) { System.out.println("Request completed: " + request); } @Override public void cancelled() { } }); queue.add(future); } while (!queue.isEmpty()) { Future<Content> future = queue.remove(); try { future.get(); } catch (ExecutionException ex) { } } System.out.println("Done"); threadpool.shutdown(); }
From source file:interoperabilite.webservice.fluent.FluentResponseHandling.java
public static void main(String[] args) throws Exception { Document result = Request.Get("http://somehost/content").execute() .handleResponse(new ResponseHandler<Document>() { @Override/*from w w w .ja va2 s. co m*/ public Document handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); ContentType contentType = ContentType.getOrDefault(entity); if (!contentType.equals(ContentType.APPLICATION_XML)) { throw new ClientProtocolException("Unexpected content type:" + contentType); } Charset charset = contentType.getCharset(); if (charset == null) { charset = Consts.ISO_8859_1; } return docBuilder.parse(entity.getContent(), charset.name()); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } }); // Do something useful with the result System.out.println(result); }
From source file:interoperabilite.webservice.fluent.FluentExecutor.java
public static void main(String[] args) throws Exception { Executor executor = Executor.newInstance().auth(new HttpHost("somehost"), "username", "password") .auth(new HttpHost("myproxy", 8080), "username", "password") .authPreemptive(new HttpHost("myproxy", 8080)); // Execute a GET with timeout settings and return response content as String. executor.execute(Request.Get("http://somehost/").connectTimeout(1000).socketTimeout(1000)).returnContent() .asString();/*from w w w .ja v a 2s. c o m*/ // Execute a POST with the 'expect-continue' handshake, using HTTP/1.1, // containing a request body as String and return response content as byte array. executor.execute(Request.Post("http://somehost/do-stuff").useExpectContinue().version(HttpVersion.HTTP_1_1) .bodyString("Important stuff", ContentType.DEFAULT_TEXT)).returnContent().asBytes(); // Execute a POST with a custom header through the proxy containing a request body // as an HTML form and save the result to the file executor.execute(Request.Post("http://somehost/some-form").addHeader("X-Custom-header", "stuff") .viaProxy(new HttpHost("myproxy", 8080)) .bodyForm(Form.form().add("username", "vip").add("password", "secret").build())) .saveContent(new File("result.dump")); }
From source file:net.airvantage.sample.AirVantageExampleFlow2.java
public static void main(String[] args) { String apiUrl = "https://na.airvantage.net/api"; // Replace these with your own api key and secret String apiKey = "your_app_id"; String apiSecret = "your_api_secret"; String login = null;/*from w ww . ja va 2 s . c o m*/ String password = null; String access_token = null; Scanner in = new Scanner(System.in); System.out.println("=== AirVantage's OAuth Workflow ==="); System.out.println(); // Obtain User/Password System.out.println("Enter your login:"); System.out.print(">>"); login = in.nextLine(); System.out.println(); System.out.println("...and your password:"); System.out.print(">>"); password = in.nextLine(); System.out.println(); // Get the Access Token System.out.println("Getting the Access Token..."); System.out.println(apiUrl + "/oauth/token?grant_type=password&username=" + login + "&password=" + password + "&client_id=" + apiKey + "&client_secret=" + apiSecret); try { access_token = Request .Get(apiUrl + "/oauth/token?grant_type=password&username=" + login + "&password=" + password + "&client_id=" + apiKey + "&client_secret=" + apiSecret) .execute().handleResponse(new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } try { String content = IOUtils.toString(entity.getContent()); JSONObject result = new JSONObject(content); return result.getString("access_token"); } catch (JSONException e) { throw new ClientProtocolException("Malformed JSON", e); } } }); System.out.println("Got the Access Token!"); System.out.println("(if you're curious it looks like this: " + access_token + " )"); System.out.println(); // Now let's go and ask for a protected resource! System.out.println("Now we're going to get info about the current user..."); JSONObject result = Request.Get(apiUrl + "/v1/users/current?access_token=" + access_token).execute() .handleResponse(new ResponseHandler<JSONObject>() { public JSONObject handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } try { String content = IOUtils.toString(entity.getContent()); return new JSONObject(content); } catch (JSONException e) { throw new ClientProtocolException("Malformed JSON", e); } } }); System.out.println("Got it! Let's see what we found..."); System.out.println(); System.out.println(result.toString()); System.out.println(); System.out.println("That's it man! Go and build something awesome with AirVantage! :)"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:io.aos.protocol.http.httpcommon.FluentRequests.java
public static void main(String... args) throws Exception { // Execute a GET with timeout settings and return response content as String. Request.Get("http://somehost/").connectTimeout(1000).socketTimeout(1000).execute().returnContent() .asString();//from w w w . j a v a 2s .c o m // Execute a POST with the 'expect-continue' handshake, using HTTP/1.1, // containing a request body as String and return response content as byte array. Request.Post("http://somehost/do-stuff").useExpectContinue().version(HttpVersion.HTTP_1_1) .bodyString("Important stuff", ContentType.DEFAULT_TEXT).execute().returnContent().asBytes(); // Execute a POST with a custom header through the proxy containing a request body // as an HTML form and save the result to the file Request.Post("http://somehost/some-form").addHeader("X-Custom-header", "stuff") .viaProxy(new HttpHost("myproxy", 8080)) .bodyForm(Form.form().add("username", "vip").add("password", "secret").build()).execute() .saveContent(new File("result.dump")); }
From source file:io.aos.protocol.http.httpcommon.FluentAsync.java
public static void main(String... args) throws Exception { // Use pool of two threads ExecutorService threadpool = Executors.newFixedThreadPool(2); Async async = Async.newInstance().use(threadpool); Request[] requests = new Request[] { Request.Get("http://www.google.com/"), Request.Get("http://www.yahoo.com/"), Request.Get("http://www.apache.com/"), Request.Get("http://www.apple.com/") }; Queue<Future<Content>> queue = new LinkedList<Future<Content>>(); // Execute requests asynchronously for (final Request request : requests) { Future<Content> future = async.execute(request, new FutureCallback<Content>() { public void failed(final Exception ex) { System.out.println(ex.getMessage() + ": " + request); }//from w w w. j a v a 2 s.c o m public void completed(final Content content) { System.out.println("Request completed: " + request); } public void cancelled() { } }); queue.add(future); } while (!queue.isEmpty()) { Future<Content> future = queue.remove(); try { future.get(); } catch (ExecutionException ex) { } } System.out.println("Done"); threadpool.shutdown(); }