List of usage examples for org.apache.http.impl.client HttpClients createDefault
public static CloseableHttpClient createDefault()
From source file:com.football.site.getdata.ScoreWebService.java
private static String GetHttpClientResponse(String url) { String responseText = ""; try {/* ww w .j a v a2s. c om*/ try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet httpGet = new HttpGet(url); httpGet.addHeader("X-Auth-Token", Constants.XAuthToken); httpGet.addHeader("Content-type", "application/json"); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); responseText = EntityUtils.toString(httpResponse.getEntity(), "ISO-8859-1"); //logger.info(String.format("%s - %s", url, responseText)); } } catch (IOException | ParseException e) { HelperUtil.AddErrorLog(logger, e); HelperUtil.AddErrorLogWithString(logger, url); } return responseText; }
From source file:eu.ebbitsproject.peoplemanager.utils.HttpUtils.java
public static void postData(String url, String action, String errorType, String objectId, String personId, String eventId) {/* w w w.java 2s .c o m*/ CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("action", action)); nvps.add(new BasicNameValuePair("personId", personId)); nvps.add(new BasicNameValuePair("errorType", errorType)); nvps.add(new BasicNameValuePair("objectId", objectId)); nvps.add(new BasicNameValuePair("eventId", eventId)); System.out.println("action = " + action); System.out.println("personId = " + personId); System.out.println("errorType = " + errorType); System.out.println("objectId = " + objectId); System.out.println("eventId = " + eventId); try { httpPost.setEntity(new UrlEncodedFormEntity(nvps)); } catch (UnsupportedEncodingException ex) { Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, ex); } CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } catch (IOException e) { Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, e); } finally { try { response.close(); } catch (IOException e) { Logger.getLogger(HttpUtils.class.getName()).log(Level.SEVERE, null, e); } } }
From source file:com.zhch.example.commons.http.v4_5.ClientExecuteProxy.java
public static void proxyExample() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w. j a v a 2 s . c o m*/ HttpHost proxy = new HttpHost("24.157.37.61", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("http://www.ip.cn"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1"); request.setConfig(config); System.out.println( "Executing request " + request.getRequestLine() + " to " + request.getURI() + " via " + proxy); CloseableHttpResponse response = httpclient.execute(request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity(), "utf8")); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:httpclient.HttpBenchmark.java
private static HttpClient createRequestExecutor() { HttpClient httpclient = HttpClients.createDefault(); // httpclient.getParams().setVersion(HttpVersion.HTTP_1_1); // httpclient.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); // httpclient.getHttpConnectionManager().getParams().setStaleCheckingEnabled(false); return httpclient; }
From source file:core.VirusTotalAPIHelper.java
public static CloseableHttpResponse reScan(String sha256) { try {/*w ww . j av a2 s . c o m*/ CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpsScanFilePost = new HttpPost(httpsPost + "file/rescan"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("apikey", apiKey); builder.addTextBody("resource", sha256); HttpEntity multipart = builder.build(); httpsScanFilePost.setEntity(multipart); CloseableHttpResponse response = client.execute(httpsScanFilePost); return response; } catch (IOException ex) { Exceptions.printStackTrace(ex); } return null; }
From source file:com.nibss.util.Request.java
public void get(String url) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w.j a v a 2s .c om*/ HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response1 = httpclient.execute(httpGet); try { HttpEntity entity1 = response1.getEntity(); EntityUtils.consume(entity1); } finally { response1.close(); } } finally { httpclient.close(); } }
From source file:text_analytics.OpenCalaisKE.java
public OpenCalaisKE() { httpClient = HttpClients.createDefault(); }
From source file:com.aws.sampleImage.url.TestHttpGetFlickrAPI.java
private static String callFlickrAPIForEachKeyword(String query) throws IOException, JSONException { String apiKey = "ad4f88ecfd53b17f93178e19703fe00d"; String apiSecret = "96cab0e9f89468d6"; int totalPages = 4; int total = 500; int perPage = 500; int counter = 0; int currentCount = 0; for (int i = 1; i <= totalPages && currentCount <= total; i++, currentCount = currentCount + perPage) { String url = "https://api.flickr.com/services/rest/?method=flickr.photos.search&text=" + query + "&extras=url_c,url_m,url_n,license,owner_name&per_page=500&page=" + i + "&format=json&api_key=" + apiKey + "&api_secret=" + apiSecret + "&license=4,5,6,7,8"; System.out.println("URL FORMED --> " + url); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); System.out.println("GET Response Status:: " + httpResponse.getStatusLine().getStatusCode()); BufferedReader reader = new BufferedReader( new InputStreamReader(httpResponse.getEntity().getContent())); String inputLine;/*from www.jav a 2 s.co m*/ StringBuffer response = new StringBuffer(); while ((inputLine = reader.readLine()) != null) { response.append(inputLine); } reader.close(); String responseString = response.toString(); responseString = responseString.replace("jsonFlickrApi(", ""); int length = responseString.length(); responseString = responseString.substring(0, length - 1); // print result System.out.println("After making it a valid JSON --> " + responseString); httpClient.close(); JSONObject json = null; try { json = new JSONObject(responseString); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println("Converted JSON String " + json); JSONObject photosObj = json.has("photos") ? json.getJSONObject("photos") : null; total = photosObj.has("total") ? (Integer.parseInt(photosObj.getString("total"))) : 0; perPage = photosObj.has("perpage") ? (Integer.parseInt(photosObj.getString("perpage"))) : 0; System.out.println(" perPage --> " + perPage + " total --> " + total); JSONArray photoArr = photosObj.getJSONArray("photo"); System.out.println("Length of Array --> " + photoArr.length()); String scrapedImageURL = ""; for (int itr = 0; itr < photoArr.length(); itr++) { JSONObject tempObject = photoArr.getJSONObject(itr); scrapedImageURL = tempObject.has("url_c") ? tempObject.getString("url_c") : tempObject.has("url_m") ? tempObject.getString("url_m") : tempObject.has("url_n") ? tempObject.getString("url_n") : ""; //System.out.println("Scraped Image URL --> " + scrapedImageURL); counter++; } } System.out.println("C O U N T E R -> " + counter); return null; }
From source file:com.javaquery.aws.elasticsearch.AddUpdateExample.java
/** * Perform post request./*from ww w . j a v a 2s. c o m*/ * @param httpPost */ public static void httpPostRequest(HttpPost httpPost) { /* Create object of CloseableHttpClient */ CloseableHttpClient httpClient = HttpClients.createDefault(); /* Response handler for after request execution */ ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { /* Get status code */ int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { /* Convert response to String */ HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; try { /* Execute URL and attach after execution response handler */ String strResponse = httpClient.execute(httpPost, responseHandler); /* Print the response */ System.out.println("Response: " + strResponse); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.kingmed.dp.ndp.NDPCommunicator.java
public NDPCommunicator() { httpClient = HttpClients.createDefault(); }