List of usage examples for org.apache.http.impl.client HttpClients createDefault
public static CloseableHttpClient createDefault()
From source file:org.apache.bigtop.bigpetstore.qstream.Utils.java
public static HttpResponse get(URI uri) throws Exception { HttpGet httppost = new HttpGet(uri); HttpClient httpclient = HttpClients.createDefault(); //Execute and get the response. try {//from w ww .j a v a 2 s . co m HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() != 200) System.err.println("FAILURE! " + response.getStatusLine().getStatusCode()); return response; } catch (Throwable t) { System.out.println("failed, sleeping"); Thread.sleep(10000); } System.err.println("FAILURE getting URI " + uri.toASCIIString()); return null; }
From source file:cn.austin.crawle.Crawler.java
public String crawleTheContent(int currpage) throws IOException { String content = null;// w w w . j a v a2 s . c o m CloseableHttpResponse response = null; InputStream is = null; try { CloseableHttpClient httpclient = HttpClients.createDefault(); int start = (currpage - 1) * 14 + 1; String url = "https://book.douban.com/subject_search?start=%s&search_text=%E7%BC%96%E7%A8%8B&cat=1001" .replace("%s", String.valueOf(start)); HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); httpGet.addHeader("Accept-Encoding", "gzip, deflate, sdch, br"); httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8"); httpGet.addHeader("Connection", "keep-alive"); httpGet.addHeader("Cookie", "gr_user_id=f2ae551f-d8be-472d-a17c-fb427123b958; viewed=\"1470240_11386364\"; ll=\"108296\"; bid=DT2w_1e4hnY; _vwo_uuid_v2=A0ADC053693BC0BAB806B37B77D3EE2E|58055d5c7afc6b50c4901fc93157dfdd; _pk_id.100001.3ac3=69b82fcb3d85b9cb.1481024949.4.1481087406.1481074949.; __utma=30149280.413198661.1441366971.1481074927.1481087106.26; __utmc=30149280; __utmz=30149280.1480767548.22.22.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utma=81379588.563439035.1481024949.1481074927.1481087107.4; __utmc=81379588; __utmz=81379588.1481024949.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"); httpGet.addHeader("Host", "book.douban.com"); httpGet.addHeader("Referer", "https://book.douban.com/"); httpGet.addHeader("Upgrade-Insecure-Requests", "1"); httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); is = entity.getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder result = new StringBuilder(); String read = null; while ((read = bufferedReader.readLine()) != null) { result.append(read); } content = result.toString(); } finally { response.close(); is.close(); } return content; }
From source file:it.av.fac.driver.APIClient.java
public String queryRequest(String userToken, String resource) { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(endpoint + QUERY_PATH); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("request", URLEncoder.encode(resource, "UTF-8"))); nvps.add(new BasicNameValuePair("token", URLEncoder.encode(userToken, "UTF-8"))); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); // Create a custom response handler ResponseHandler<String> responseHandler = (final HttpResponse response) -> { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); }//w w w . j a va2 s.c om }; return httpclient.execute(httpPost, responseHandler); } catch (IOException ex) { Logger.getLogger(APIClient.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:org.sead.repositories.reference.util.SEADAuthenticator.java
static public HttpClientContext authenticate(String server) { boolean authenticated = false; log.info("Authenticating"); String accessToken = SEADGoogleLogin.getAccessToken(); // Now login to server and create a session CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w w w . j a v a 2 s. c o m // //DoLogin is the auth endpoint when using the AUthFilter, versus // the api/authenticate endpoint when connecting to the ACR directly HttpPost seadAuthenticate = new HttpPost(server + "/DoLogin"); List<NameValuePair> nvpList = new ArrayList<NameValuePair>(1); nvpList.add(0, new BasicNameValuePair("googleAccessToken", accessToken)); seadAuthenticate.setEntity(new UrlEncodedFormEntity(nvpList)); CloseableHttpResponse response = httpclient.execute(seadAuthenticate, localContext); try { if (response.getStatusLine().getStatusCode() == 200) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { // String seadSessionId = // EntityUtils.toString(resEntity); authenticated = true; } } else { // Seems to occur when google device id is not set on server // - with a Not Found response... log.error("Error response from " + server + " : " + response.getStatusLine().getReasonPhrase()); } } finally { response.close(); httpclient.close(); } } catch (IOException e) { log.error("Cannot read sead-google.json"); log.error(e.getMessage()); } // localContext should have the cookie with the SEAD session key, which // nominally is all that's needed. // FixMe: If there is no activity for more than 15 minutes, the session // may expire, in which case, // re-authentication using the refresh token to get a new google token // to allow SEAD login again may be required // also need to watch the 60 minutes google token timeout - project // spaces will invalidate the session at 60 minutes even if there is // activity authTime = System.currentTimeMillis(); if (authenticated) { return localContext; } return null; }
From source file:org.nuxeo.elasticsearch.http.readonly.HttpClient.java
public static String get(String url) throws IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); try (CloseableHttpResponse response = client.execute(httpget)) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; }// w w w . j a v a 2s. co m }
From source file:fr.lissi.belilif.om2m.rest.WebServiceActions.java
/** * Do get./*from www . jav a2s .c o m*/ * * @param uri * the uri * @param headers * the headers * @return the string * @throws Exception * the exception */ public static String doGet(URI uri, HashMap<String, String> headers) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); String respString = null; try { /* * HttpClient provides URIBuilder utility class to simplify creation and modification of request URIs. * * URI uri = new URIBuilder() .setScheme("http") .setHost("hc.apache.org/") // .setPath("/search") // .setParameter("q", * "httpclient") // .setParameter("btnG", "Google Search") // .setParameter("aq", "f") // .setParameter("oq", "") .build(); */ HttpGet httpGet = new HttpGet(uri); for (String key : headers.keySet()) { httpGet.addHeader(key, headers.get(key)); } CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try { System.out.println(response1.getStatusLine()); HttpEntity entity = response1.getEntity(); // do something useful with the response body if (entity != null) { respString = EntityUtils.toString(entity); } // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response1.close(); } } finally { httpclient.close(); } return respString; }
From source file:com.nibss.util.Request.java
public void post(String url, List<NameValuePair> list) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*ww w .j ava 2s . c o m*/ HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(list)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine().getStatusCode()); HttpEntity entity2 = response2.getEntity(); EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:kmi.taa.core.SPARQLHTTPClient.java
public String httpGet(String url, String proxy) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); String responseBody;/*w w w . ja v a2 s .c o m*/ try { if (!proxy.isEmpty()) { String[] str = proxy.split(":"); int port = Integer.parseInt(str[1]); HttpHost host = new HttpHost(str[0], port, str[2]); RequestConfig config = RequestConfig.custom().setProxy(host).build(); httpget.setConfig(config); } ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { try { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity, StandardCharsets.UTF_8) : null; } catch (ClientProtocolException e) { return ""; } } return ""; } }; responseBody = httpclient.execute(httpget, responseHandler); } finally { httpclient.close(); } return responseBody; }
From source file:com.google.appengine.tck.misc.http.support.Client.java
protected synchronized HttpClient getClient() { if (client == null) { client = HttpClients.createDefault(); } return client; }
From source file:de.qaware.chronix.solr.retention.RetentionJob.java
/** * Constructs a retention job */ public RetentionJob() { httpClient = HttpClients.createDefault(); }