List of usage examples for org.apache.http.impl.client HttpClients custom
public static HttpClientBuilder custom()
From source file:org.tinymediamanager.scraper.util.TmmHttpClient.java
/** * instantiates a new CloseableHttpClient * //from w w w. j av a 2s . c o m * @return CloseableHttpClient */ public static CloseableHttpClient createHttpClient() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); // Increase default max connection per route to 5 connectionManager.setMaxTotal(5); HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); httpClientBuilder.setConnectionManager(connectionManager); // my own retry handler HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; httpClientBuilder.setRetryHandler(myRetryHandler); // set proxy if needed if ((Globals.settings.useProxy())) { setProxy(httpClientBuilder); } return httpClientBuilder.build(); }
From source file:org.neo4j.ogm.service.ComponentsTest.java
@Test public void shouldCustomiseHttpDriverClient() { if (Components.driver() instanceof HttpDriver) { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(1); connectionManager.setDefaultMaxPerRoute(1); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build(); Driver driver = new HttpDriver(httpClient); Components.setDriver(driver);/* w w w.j av a 2 s . co m*/ Assert.assertEquals(driver, Components.driver()); } }
From source file:guru.nidi.ramlproxy.core.CommandSender.java
protected HttpClient createClient() { final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000) .setConnectionRequestTimeout(1000).setSocketTimeout(1000).build(); return HttpClients.custom().setDefaultRequestConfig(requestConfig).build(); }
From source file:justdailscrapper.vik.utility.FetchPageWithoutProxy.java
public static String fetchPageSourcefromClientGoogle(URI newurl, List<ProxyImport> proxyList) throws IOException, InterruptedException { Random r = new Random(); ProxyImport obj = proxyList.get(r.nextInt(proxyList.size())); String ip = obj.proxyIP;/*from www .jav a2s .c o m*/ int portno = Integer.parseInt(obj.proxyPort); String username = ""; String password = ""; if (obj.proxyLen > 2) { username = obj.proxyUserName; password = obj.proxyPassword; } // int portNo = generateRandomPort(); CredentialsProvider credsprovider = new BasicCredentialsProvider(); credsprovider.setCredentials(new AuthScope(ip, portno), new UsernamePasswordCredentials(username, password)); HttpHost proxy = new HttpHost(ip, portno); //----------------------------------------------------------------------- RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsprovider) .setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0") .setDefaultRequestConfig(requestConfig).setProxy(proxy).build(); String responsebody = ""; String responsestatus = null; int count = 0; try { HttpGet httpget = new HttpGet(newurl); httpget.addHeader("Accept", "*/*"); httpget.addHeader("Accept-Encoding", "gzip, deflate, br"); httpget.addHeader("Accept-Language", "en-US,en;q=0.5"); httpget.addHeader("Host", "www.justdial.com"); httpget.addHeader("Referer", "https://www.justdial.com/Bangalore/Yamaha-Two-Wheeler-Dealers"); httpget.addHeader("Connection", "keep-alive"); httpget.addHeader("X-Requested-With", "XMLHttpReques"); System.out.println("Response status " + httpget.getRequestLine()); CloseableHttpResponse resp = httpclient.execute(httpget); responsestatus = resp.getStatusLine().toString(); if (responsestatus.contains("503") || responsestatus.contains("502") || responsestatus.contains("403") || responsestatus.contains("400") || responsestatus.contains("407") || responsestatus.contains("401") || responsestatus.contains("402") || responsestatus.contains("404") || responsestatus.contains("405") || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999") || responsestatus.contains("ClientProtocolException") || responsestatus.contains("SocketTimeoutException") || "".equals(responsestatus)) { Thread.sleep(10000); do { count++; responsebody = fetchPageSourcefromClientGoogleSecond(newurl, proxyList); if (responsebody == null) { Thread.sleep(10000); System.out.println("PROX FAILURE"); } if (count > 20) { Thread.sleep(1000); break; } } while (responsebody == null || "".equals(responsebody)); } else { HttpEntity entity = resp.getEntity(); System.out.println(resp.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); String inputLine; while ((inputLine = in.readLine()) != null) { responsebody = new StringBuilder().append(responsebody).append(inputLine).toString(); } // writeResponseFile(responsebody, pagename); } EntityUtils.consume(entity); } } catch (IOException | IllegalStateException e) { System.out.println("Exception = " + e); do { count++; responsebody = fetchPageSourcefromClientGoogleSecond(newurl, proxyList); if (responsebody == null) { System.out.println("PROX FAILURE"); } if (count > 15) { Thread.sleep(50000); // responsebody = fetchPageSourcefromClientGoogleSecond(newurl); break; } } while (responsebody == null || "".equals(responsebody)); } finally { httpclient.close(); } return responsebody; }
From source file:com.yahoo.gondola.container.client.ApacheHttpComponentProxyClient.java
public ApacheHttpComponentProxyClient() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(200);//from w w w. j a v a2 s .c om cm.setDefaultMaxPerRoute(200); httpClient = HttpClients.custom().setConnectionManager(cm).build(); }
From source file:org.apache.jena.jdbc.remote.connections.TestRemoteEndpointConnectionWithAuth.java
/** * Setup for the tests by allocating a Fuseki instance to work with * @throws IOException /*www .j a va 2s . com*/ */ @BeforeClass public static void setup() throws IOException { SecurityHandler sh = FusekiTestAuth.makeSimpleSecurityHandler("/*", USER, PASSWORD); FusekiTestAuth.setupServer(true, sh); BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); credsProv.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASSWORD)); client = HttpClients.custom().setDefaultCredentialsProvider(credsProv).build(); }
From source file:org.riotfamily.linkcheck.HttpStatusChecker.java
public HttpStatusChecker() { RequestConfig config = RequestConfig.custom() .setConnectionRequestTimeout((int) FormatUtils.parseMillis("2s")) .setSocketTimeout((int) FormatUtils.parseMillis("5s")).setStaleConnectionCheckEnabled(true).build(); client = HttpClients.custom().setDefaultRequestConfig(config).build(); }
From source file:de.cuseb.bilderbuch.helpers.HttpClientProviderService.java
public HttpClient getHttpClientWithBasicAuth(String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); }
From source file:justdailscrapper.vik.utility.FetchPageWithProxy.java
public static String fetchPageSourcefromClientGoogle(URI newurl, List<ProxyImport> proxyList) throws IOException, InterruptedException { Random r = new Random(); ProxyImport obj = proxyList.get(r.nextInt(proxyList.size())); String ip = obj.proxyIP;//from w w w . ja v a 2s .c o m int portno = Integer.parseInt(obj.proxyPort); String username = ""; String password = ""; if (obj.proxyLen > 2) { username = obj.proxyUserName; password = obj.proxyPassword; } // int portNo = generateRandomPort(); CredentialsProvider credsprovider = new BasicCredentialsProvider(); credsprovider.setCredentials(new AuthScope(ip, portno), new UsernamePasswordCredentials(username, password)); HttpHost proxy = new HttpHost(ip, portno); //----------------------------------------------------------------------- RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsprovider) .setUserAgent("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0") .setDefaultRequestConfig(requestConfig).setProxy(proxy).build(); String responsebody = ""; String responsestatus = null; int count = 0; try { HttpGet httpget = new HttpGet(newurl); httpget.addHeader("Accept", "*/*"); httpget.addHeader("Accept-Encoding", "gzip, deflate, br"); httpget.addHeader("Accept-Language", "en-US,en;q=0.5"); httpget.addHeader("Host", "www.justdial.com"); httpget.addHeader("Referer", "https://www.justdial.com/Bangalore/Yamaha-Two-Wheeler-Dealers"); httpget.addHeader("Connection", "keep-alive"); httpget.addHeader("X-Requested-With", "XMLHttpReques"); System.out.println("Response status " + httpget.getRequestLine()); CloseableHttpResponse resp = httpclient.execute(httpget); responsestatus = resp.getStatusLine().toString(); if (responsestatus.contains("503") || responsestatus.contains("502") || responsestatus.contains("403") || responsestatus.contains("400") || responsestatus.contains("407") || responsestatus.contains("401") || responsestatus.contains("402") || responsestatus.contains("404") || responsestatus.contains("405") || responsestatus.contains("SSLHandshakeException") || responsestatus.contains("999") || responsestatus.contains("ClientProtocolException") || responsestatus.contains("SocketTimeoutException") || "".equals(responsestatus)) { Thread.sleep(10000); do { count++; responsebody = fetchPageSourcefromClientGoogleSecond(newurl, proxyList); if (responsebody == null) { Thread.sleep(10000); logTextArea.append("PROX FAILURE\n"); } if (count > 20) { Thread.sleep(1000); break; } } while (responsebody == null || "".equals(responsebody)); } else { HttpEntity entity = resp.getEntity(); System.out.println(resp.getStatusLine()); if (entity != null) { logTextArea.append("Response content length: " + entity.getContentLength() + "\n"); BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); String inputLine; while ((inputLine = in.readLine()) != null) { responsebody = new StringBuilder().append(responsebody).append(inputLine).toString(); } // writeResponseFile(responsebody, pagename); } EntityUtils.consume(entity); } } catch (IOException | IllegalStateException e) { System.out.println("Exception = " + e); do { count++; responsebody = fetchPageSourcefromClientGoogleSecond(newurl, proxyList); if (responsebody == null) { System.out.println("PROX FAILURE"); } if (count > 15) { Thread.sleep(50000); // responsebody = fetchPageSourcefromClientGoogleSecond(newurl); break; } } while (responsebody == null || "".equals(responsebody)); } finally { httpclient.close(); } return responsebody; }
From source file:de.intevation.irix.PrintClient.java
/** Obtains a Report from mapfish-print service. * * @param printUrl The url to send the request to. * @param json The json spec for the print request. * @param timeout the timeout for the httpconnection. * * @return byte[] with the report./* ww w . j av a 2 s .co m*/ * * @throws IOException if communication with print service failed. * @throws PrintException if the print job failed. */ public static byte[] getReport(String printUrl, String json, int timeout) throws IOException, PrintException { RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).build(); CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(config).build(); HttpEntity entity = new StringEntity(json, ContentType.create("application/json", Charset.forName("UTF-8"))); HttpPost post = new HttpPost(printUrl); post.setEntity(entity); CloseableHttpResponse resp = client.execute(post); StatusLine status = resp.getStatusLine(); byte[] retval = null; try { HttpEntity respEnt = resp.getEntity(); InputStream in = respEnt.getContent(); if (in != null) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[BYTE_ARRAY_SIZE]; int r; while ((r = in.read(buf)) >= 0) { out.write(buf, 0, r); } retval = out.toByteArray(); } finally { in.close(); EntityUtils.consume(respEnt); } } } finally { resp.close(); } if (status.getStatusCode() < HttpURLConnection.HTTP_OK || status.getStatusCode() >= HttpURLConnection.HTTP_MULT_CHOICE) { if (retval != null) { throw new PrintException(new String(retval)); } else { throw new PrintException("Communication with print service '" + printUrl + "' failed." + "\nNo response from print service."); } } return retval; }