List of usage examples for org.apache.http.client.config RequestConfig custom
public static RequestConfig.Builder custom()
From source file:com.kolich.http.HttpClient4ClosureBuilder.java
/** * Creates a new {@link HttpClient} global {@link RequestConfig} object. * The {@link RequestConfig} object is where request specific settings * like socket and connection timeouts live. *//*from ww w . j av a 2 s. c om*/ public RequestConfig getRequestConfig() { return RequestConfig.custom().setSocketTimeout(socketTimeout_).setConnectTimeout(connectTimeout_) .setConnectionRequestTimeout(connectTimeout_) // When this is set to false, this apparently causes bugs // with the PoolingHttpClientConnectionManager in 4.3.3. It's // unclear to me why, but I'm leaving this 'true' although // claims are made it adds 30ms of lag time to every request. .setStaleConnectionCheckEnabled(staleConnectionCheckEnabled_).build(); }
From source file:org.alfresco.cacheserver.http.CacheHttpClient.java
private CloseableHttpClient getHttpClient(HttpHost target, HttpClientContext localContext, String username, String password) {/*from w w w . j a va 2 s . co m*/ ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // Honor 'keep-alive' header HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } HttpHost target = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST); if ("www.naughty-server.com".equalsIgnoreCase(target.getHostName())) { // Keep alive for 5 seconds only return 5 * 1000; } else { // otherwise keep alive for 30 seconds return 30 * 1000; } } }; HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { 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; } }; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password)); RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true) // .setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig) .setDefaultCredentialsProvider(credsProvider).setKeepAliveStrategy(keepAliveStrategy) .setRetryHandler(retryHandler).build(); return httpclient; }
From source file:com.ibm.devops.notification.MessageHandlerTest.java
@Test public void testPostToWebhook() throws IOException { HttpClientBuilder httpClientBuilder = PowerMockito.mock(HttpClientBuilder.class); CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class); CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class); PowerMockito.mockStatic(HttpClients.class); StatusLine statusLine = mock(StatusLine.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(baos); String content;/*from ww w . j a va 2 s. c o m*/ JSONObject message = new JSONObject(); RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).build(); when(HttpClients.custom()).thenReturn(httpClientBuilder); when(httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig).build()).thenReturn(httpClient); when(httpClient.execute(any(HttpPost.class))).thenReturn(response); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.toString()).thenReturn("200"); assertTrue(Util.isNullOrEmpty("")); assertTrue(Util.isNullOrEmpty(null)); MessageHandler.postToWebhook("", false, message, printStream); content = new String(baos.toByteArray(), StandardCharsets.UTF_8); assertTrue(content.contains("[IBM Cloud DevOps] IBM_CLOUD_DEVOPS_WEBHOOK_URL not set.")); MessageHandler.postToWebhook("http://fakewebhook", false, message, printStream); content = new String(baos.toByteArray(), StandardCharsets.UTF_8); assertTrue(content.contains("[IBM Cloud DevOps] Message successfully posted to webhook.")); when(statusLine.toString()).thenReturn("400"); MessageHandler.postToWebhook("http://fakewebhook", false, message, printStream); content = new String(baos.toByteArray(), StandardCharsets.UTF_8); assertTrue(content.contains("[IBM Cloud DevOps] Message failed, response status:")); when(httpClient.execute(any(HttpPost.class))).thenThrow(new IOException("...")); MessageHandler.postToWebhook("http://fakewebhook", false, message, printStream); content = new String(baos.toByteArray(), StandardCharsets.UTF_8); assertTrue(content.contains("[IBM Cloud DevOps] IOException, could not post to webhook:")); }
From source file:com.lehman.ic9.net.httpClient.java
/** * Default constructor takes the reference to the script engine to reference * later and the request URL string.//w w w . j a va2 s . c om * @param Eng is an ic9engine instance. * @param JsObj is a Map of String - Object with the JS instance of the httpClient for later reference. * @param ReqUrl is a String with the request URL. * @throws MalformedURLException Exception */ public httpClient(ic9engine Eng, Map<String, Object> JsObj, String ReqUrl) throws MalformedURLException { // Set engine. this.eng = Eng; this.jsobj = JsObj; this.cs = new BasicCookieStore(); this.cp = new BasicCredentialsProvider(); this.rcb = RequestConfig.custom(); this.u = new URL(ReqUrl); }
From source file:com.codeabovelab.dm.gateway.proxy.common.BalancerConfiguration.java
protected CloseableHttpAsyncClient configuredHttpAsyncClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { LOG.info("HttpAsyncClient settings: maxConnections: {}, socketTimeout: {}, connectTimeout: {}", maxConnections, socketTimeout, connectTimeout); return httpAsyncClientBuilder.setMaxConnPerRoute(maxConnections).setMaxConnTotal(maxConnections) .setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout(socketTimeout) .setConnectTimeout(connectTimeout).build()) .disableCookieManagement().build(); }
From source file:com.supernovapps.audio.jstreamsourcer.Icecast.java
@Override public void updateMetadata(String song, String artist, String album) { if (!started) { return;//from ww w.ja va 2s . com } RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout * 1000) .setConnectTimeout(timeout * 1000).build(); HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); HttpUriRequest request = getUpdateMetadataRequest(song, artist, album); try { httpClient.execute(request); } catch (IOException e) { e.printStackTrace(); } }
From source file:gui.EventReader.java
@Override public void run() { RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10 * 1000).build(); HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); //HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet( //"https://api.guildwars2.com/v1/events.json?world_id=" "http://gw2eventer.sourceforge.net/api/events.php?world_id=" + this.worldID); HttpResponse response;/*from w ww .j ava 2s .c om*/ String line = ""; String out = ""; while (!isInterrupted()) { try { this.labelWorking.setText(this.workingString); try { response = client.execute(request); if (response.getStatusLine().toString().contains("200")) { BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent(), Charset.forName("UTF-8"))); line = ""; out = ""; while ((line = rd.readLine()) != null) { out = out + line; } } else { // http error request.releaseConnection(); this.refreshSelector.setEnabled(false); this.workingButton.setEnabled(false); this.jComboBoxLanguage.setEnabled(false); this.labelWorking.setText("connection error. retrying in... 10."); this.labelWorking.setVisible(true); Thread.sleep(10000); continue; } } catch (IOException | IllegalStateException ex) { Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex); request.releaseConnection(); this.refreshSelector.setEnabled(false); this.workingButton.setEnabled(false); this.jComboBoxLanguage.setEnabled(false); this.labelWorking.setText("connection error. retrying in... 10."); this.labelWorking.setVisible(true); Thread.sleep(10000); continue; //this.interrupt(); } request.releaseConnection(); JSONParser parser = new JSONParser(); Object obj; this.result.clear(); HashMap mehrfachEvents = new HashMap(); playSoundsList.clear(); this.overlayGui.clearActive(); String toolTip; try { obj = parser.parse(out); for (int i = 0; i < this.eventLabels.size(); i++) { JLabel iter = (JLabel) this.eventLabels.get(i); iter.setEnabled(false); iter.setForeground(Color.green); } for (Iterator iterator = ((JSONObject) obj).values().iterator(); iterator.hasNext();) { JSONArray arrayNew = (JSONArray) iterator.next(); for (int i = 0; i < arrayNew.size(); i++) { JSONObject obj2 = (JSONObject) arrayNew.get(i); if (obj2.get("event_id") != null) { String event = (String) obj2.get("event_id"); if (this.events.containsKey(event)) { //System.out.println("debug: " + event + "\n"); this.result.add(obj2.get("event_id")); int indexEvent = Integer.parseInt(((String[]) this.events.get(event))[0]); String eventPercent = ((String[]) this.events.get(event))[1]; String eventWav = ((String[]) this.events.get(event))[2]; String eventName = ((String[]) this.events.get(event))[3]; JLabel activeLabel = (JLabel) this.eventLabels.get(indexEvent - 1); JLabel activeLabelTimer = (JLabel) this.eventLabelsTimer.get(indexEvent - 1); int activeLabelInt = indexEvent - 1; String tmpEventName = eventPercent.substring(0, 1); Date dateNow = new Date(); long stampNow = dateNow.getTime(); if (this.timerStamps[activeLabelInt] != null) { long oldTimestamp = this.timerStamps[activeLabelInt].getTime(); long minsdiff = ((stampNow - oldTimestamp) / 1000 / 60); if (minsdiff >= 30) { activeLabelTimer.setEnabled(true); } else { activeLabelTimer.setEnabled(false); } if (minsdiff >= 60) { activeLabelTimer.setForeground(Color.red); } else { activeLabelTimer.setForeground(Color.green); } activeLabelTimer.setText(minsdiff + " mins (B)"); } /* if (activeLabel != null) { if (activeLabel.getToolTipText() != null) { if (activeLabel.getToolTipText().equals("")) { // null pointer?? activeLabel.setToolTipText((String) this.allEvents.get(obj2.get("event_id"))); } } }*/ if (obj2.get("state").equals("Active")) { activeLabel.setEnabled(true); //activeLabel.setToolTipText((String) this.allEvents.get(obj2.get("event_id"))); toolTip = activeLabel.getToolTipText(); if (toolTip != null) { if (toolTip.length() > 35) { toolTip = toolTip.substring(0, 35) + "..."; } } else { toolTip = ""; } if (tmpEventName.equals("B")) { this.markedBs[activeLabelInt] = true; activeLabelTimer.setVisible(false); this.timerStamps[activeLabelInt] = null; if (this.eventPlaySounds[activeLabelInt][2]) { if (!this.overlayGui.containsActiveB(eventName)) { this.overlayGui.addActiveB(eventName, "yellow", activeLabelInt); } } else { if (!this.overlayGui.containsActiveB(eventName)) { this.overlayGui.addActiveB(eventName, "green", activeLabelInt); } } } else { if (this.eventPlaySounds[activeLabelInt][2]) { if (!this.overlayGui.containsActivePre(eventName)) { this.overlayGui.addActivePreEvent(eventName, "yellow", activeLabelInt); } } else { if (!this.overlayGui.containsActivePre(eventName)) { this.overlayGui.addActivePreEvent(eventName, "green", activeLabelInt); } } } //activeLabel.setSize(100, activeLabel.getSize().height); //activeLabel.setText(eventPercent); URL url = this.getClass().getClassLoader() .getResource("media/sounds/" + eventWav + ".wav"); if (!playSoundsList.containsKey(url)) { if (!this.eventPlaySounds[activeLabelInt][2]) { if (tmpEventName.equals("B")) { if (this.eventPlaySounds[activeLabelInt][1]) { playSoundsList.put(url, activeLabel); } } else { if (this.eventPlaySounds[activeLabelInt][0]) { playSoundsList.put(url, activeLabel); } } } else { activeLabel.setForeground(Color.YELLOW); } } if (mehrfachEvents.containsKey(activeLabel)) { ((ArrayList) mehrfachEvents.get(activeLabel)).add(tmpEventName); } else { ArrayList tmpListe = new ArrayList(); tmpListe.add(tmpEventName); mehrfachEvents.put(activeLabel, tmpListe); } } else { if (tmpEventName.equals("B")) { if (this.markedBs[activeLabelInt]) { this.timerStamps[activeLabelInt] = dateNow; this.markedBs[activeLabelInt] = false; activeLabelTimer.setVisible(true); activeLabelTimer.setText("0 mins (B)"); } } } } } } } Iterator it = mehrfachEvents.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); JLabel label = (JLabel) pairs.getKey(); ArrayList liste = (ArrayList) pairs.getValue(); String outString = null; Collections.sort(liste, new Comparator<String>() { public int compare(String f1, String f2) { return -f1.toString().compareTo(f2.toString()); } }); for (int i = 0; i < liste.size(); i++) { if (outString == null) { outString = (String) liste.get(i); } else { outString += ", " + (String) liste.get(i); } } label.setText(outString); it.remove(); } this.labelServer.setOpaque(true); this.labelServer.setOpaque(false); this.labelServer.setEnabled(false); this.labelServer.setText(""); this.labelServer.setText(this.worldName); this.labelWorking.setVisible(false); this.refreshSelector.setEnabled(true); this.workingButton.setEnabled(true); this.jComboBoxLanguage.setEnabled(true); this.overlayGui.renderActive(); if (playSounds) { this.playThread = new Thread() { @Override public void run() { Iterator it = playSoundsList.entrySet().iterator(); while (it.hasNext() && !isInterrupted()) { AudioInputStream audioIn; Map.Entry pairs = (Map.Entry) it.next(); JLabel label = (JLabel) pairs.getValue(); try { playSoundsCurrent = (URL) pairs.getKey(); audioIn = AudioSystem.getAudioInputStream(playSoundsCurrent); Clip clip = null; String tmp = label.getText(); try { //label.setText(">" + tmp); //label.setText("<HTML><U>" + tmp + "<U><HTML>"); label.setForeground(Color.red); //Font font = label.getFont(); //Map attributes = font.getAttributes(); //attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); //label.setFont(font.deriveFont(attributes)); clip = AudioSystem.getClip(); clip.open(audioIn); clip.start(); } catch (LineUnavailableException ex) { Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex); } finally { try { audioIn.close(); } catch (IOException ex) { Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex); } } try { Thread.sleep(2000); //label.setText(tmp); label.setForeground(Color.green); } catch (InterruptedException ex) { Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex); this.interrupt(); //?? } } catch (UnsupportedAudioFileException ex) { Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex); } it.remove(); } } }; this.playThread.start(); this.playThread.join(); } } catch (ParseException ex) { Logger.getLogger(ApiManager.class.getName()).log(Level.SEVERE, null, ex); this.refreshSelector.setEnabled(false); this.workingButton.setEnabled(false); this.jComboBoxLanguage.setEnabled(false); this.labelWorking.setText("connection error. retrying in... 10."); this.labelWorking.setVisible(true); Thread.sleep(10000); continue; } if (this.autoRefresh) { Thread.sleep(this.sleepTime * 1000); } else { this.interrupt(); } } catch (InterruptedException ex) { Logger.getLogger(ApiManager.class.getName()).log(Level.SEVERE, null, ex); this.interrupt(); } } }
From source file:com.mirth.connect.client.core.ServerConnection.java
public ServerConnection(int timeout, String[] httpsProtocols, String[] httpsCipherSuites, boolean allowHTTP) { SSLContext sslContext = null; try {/*from w w w . ja v a 2 s.c om*/ sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); } catch (Exception e) { logger.error("Unable to build SSL context.", e); } String[] enabledProtocols = MirthSSLUtil.getEnabledHttpsProtocols(httpsProtocols); String[] enabledCipherSuites = MirthSSLUtil.getEnabledHttpsCipherSuites(httpsCipherSuites); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, enabledProtocols, enabledCipherSuites, NoopHostnameVerifier.INSTANCE); RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionSocketFactory); if (allowHTTP) { builder.register("http", PlainConnectionSocketFactory.getSocketFactory()); } Registry<ConnectionSocketFactory> socketFactoryRegistry = builder.build(); PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager( socketFactoryRegistry); httpClientConnectionManager.setDefaultMaxPerRoute(5); httpClientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build()); // MIRTH-3962: The stale connection settings has been deprecated, and this is recommended instead httpClientConnectionManager.setValidateAfterInactivity(5000); HttpClientBuilder clientBuilder = HttpClients.custom().setConnectionManager(httpClientConnectionManager); HttpUtil.configureClientBuilder(clientBuilder); client = clientBuilder.build(); requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT) .setConnectionRequestTimeout(CONNECT_TIMEOUT).setSocketTimeout(timeout).build(); }
From source file:justdailscrapper.vik.utility.FetchPageWithoutProxy.java
public static String fetchPage(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 va2 s .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.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.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; }