List of usage examples for org.apache.http.client.config RequestConfig custom
public static RequestConfig.Builder custom()
From source file:com.world.watch.worldwatchcron.util.PushToUser.java
public static void push(List<String> data, String userId) { try {//from w ww . j av a 2 s . co m InputStream jsonStream = PushToUser.class.getResourceAsStream("/parsePush.json"); ObjectMapper mapper = new ObjectMapper(); PushData push = mapper.readValue(jsonStream, PushData.class); push.getWhere().setUsername(userId); push.getData().setKeywords(data); String json = mapper.writeValueAsString(push); HttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().build()) .build(); HttpPost post = new HttpPost(URL); post.setHeader("X-Parse-Application-Id", "WhqWj009luOxOtIH3rM9iWJICLdf0NKbgqdaui8Q"); post.setHeader("X-Parse-REST-API-Key", "lThhKObAz1Tkt092Cl1HeZv4KLUsdATvscOaGN2y"); post.setHeader("Content-Type", "application/json"); logger.debug("JSON to push {}", json.toString()); StringEntity strEntity = new StringEntity(json); post.setEntity(strEntity); httpClient.execute(post); logger.debug("Pushed {} to userId {}", data.toString(), userId); } catch (Exception ex) { logger.error("Push Failed for {} ", userId, ex); } }
From source file:com.github.yongchristophertang.engine.web.WebTemplateBuilder.java
/** * Build a webTemplate with custom config * * @return {@link WebTemplateBuilder}/*from www . j av a 2 s .c o m*/ */ public static WebTemplateBuilder customConfig() { return new WebTemplateBuilder(RequestConfig.custom()); }
From source file:com.srotya.sidewinder.cluster.Utils.java
/** * Build a {@link CloseableHttpClient}// w w w . j a v a 2 s .c om * * @param baseURL * @param connectTimeout * @param requestTimeout * @return http client * @throws NoSuchAlgorithmException * @throws KeyStoreException * @throws KeyManagementException */ public static CloseableHttpClient buildClient(String baseURL, int connectTimeout, int requestTimeout) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { HttpClientBuilder clientBuilder = HttpClients.custom(); RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(requestTimeout).build(); return clientBuilder.setDefaultRequestConfig(config).build(); }
From source file:org.drftpd.util.HttpUtils.java
public static String retrieveHttpAsString(String url) throws HttpException, IOException { RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setUserAgent(_userAgent).build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig);//from www . j a v a2 s . c o m CloseableHttpResponse response = null; try { response = httpclient.execute(httpGet); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new HttpException("Error " + statusCode + " for URL " + url); } HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); EntityUtils.consume(entity); return data; } catch (IOException e) { throw new IOException("Error for URL " + url, e); } finally { if (response != null) { response.close(); } httpclient.close(); } }
From source file:org.nuxeo.connect.connector.http.ProxyHelper.java
public static void configureProxyIfNeeded(HttpClientBuilder httpClientBuilder, String url) { RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); ProxyHelper.configureProxyIfNeeded(requestConfigBuilder, credentialsProvider, url); httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }
From source file:com.afrisoftech.lib.PDF2ExcelConverter.java
public static void convertPDf2Excel(String pdfFile2Convert) throws Exception { if (pdfFile2Convert.length() < 3) { System.out.println("File to convert is mandatory!"); javax.swing.JOptionPane.showMessageDialog(null, "File to convert is mandatory!"); } else {/*from w w w .ja v a2s .co m*/ final String apiKey = "ktxpfvf0i5se"; final String format = "xlsx-single".toLowerCase(); final String pdfFilename = pdfFile2Convert; if (!formats.contains(format)) { System.out.println("Invalid output format: \"" + format + "\""); } // Avoid cookie warning with default cookie configuration RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build(); File inputFile = new File(pdfFilename); if (!inputFile.canRead()) { System.out.println("Can't read input PDF file: \"" + pdfFilename + "\""); javax.swing.JOptionPane.showMessageDialog(null, "File to convert is mandatory!"); } try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig) .build()) { HttpPost httppost = new HttpPost("https://pdftables.com/api?format=" + format + "&key=" + apiKey); FileBody fileBody = new FileBody(inputFile); HttpEntity requestBody = MultipartEntityBuilder.create().addPart("f", fileBody).build(); httppost.setEntity(requestBody); System.out.println("Sending request"); try (CloseableHttpResponse response = httpclient.execute(httppost)) { if (response.getStatusLine().getStatusCode() != 200) { System.out.println(response.getStatusLine()); javax.swing.JOptionPane.showMessageDialog(null, "Internet connection is a must. Consult IT administrator for further assistance"); } HttpEntity resEntity = response.getEntity(); if (resEntity != null) { final String outputFilename = getOutputFilename(pdfFilename, format.replaceFirst("-.*$", "")); System.out.println("Writing output to " + outputFilename); final File outputFile = new File(outputFilename); FileUtils.copyInputStreamToFile(resEntity.getContent(), outputFile); if (java.awt.Desktop.isDesktopSupported()) { try { java.awt.Desktop.getDesktop().open(outputFile); } catch (IOException ex) { javax.swing.JOptionPane.showMessageDialog(new java.awt.Frame(), ex.getMessage()); ex.printStackTrace(); //Exceptions.printStackTrace(ex); } } } else { System.out.println("Error: file missing from response"); javax.swing.JOptionPane.showMessageDialog(null, "Error: file missing from response! Internet connection is a must."); } } } } }
From source file:com.clonephpscrapper.utility.FetchPageWithProxy.java
public static String fetchPageSourcefromClientGoogle(URI newurl) throws IOException, InterruptedException { int portNo = generateRandomPort(); CredentialsProvider credsprovider = new BasicCredentialsProvider(); credsprovider.setCredentials(new AuthScope("195.154.161.103", portNo), new UsernamePasswordCredentials("mongoose", "Fjh30fi")); HttpHost proxy = new HttpHost("195.154.161.103", 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 {/*from w w w .j a va 2s. c o m*/ HttpGet httpget = new HttpGet(newurl); httpget.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpget.addHeader("Accept-Encoding", "gzip, deflate"); httpget.addHeader("Accept-Language", "en-US,en;q=0.5"); httpget.addHeader("Connection", "keep-alive"); 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); 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); 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:v2.service.generic.library.utils.HttpClientUtil.java
public static HttpClient createHttpClient() { RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10 * 1000).build(); CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); return httpclient; }
From source file:com.johnson.grab.browser.HttpClientUtil.java
/** * Get content by url as string// w ww . ja va 2 s . c om * @param url * original url * @return * page content * @throws java.io.IOException */ public static String getContent(String url) throws CrawlException, IOException { // construct request HttpGet request = new HttpGet(url); request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT) .setSocketTimeout(SOCKECT_TIMEOUT).build()); // construct response handler ResponseHandler<String> handler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws IOException { StatusLine status = response.getStatusLine(); // status if (status.getStatusCode() != HttpStatus.SC_OK) { throw new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()); } // get encoding in header String encoding = getPageEncoding(response); boolean encodingFounded = true; if (StringUtil.isEmpty(encoding)) { encodingFounded = false; encoding = UniversalConstants.Encoding.ISO_8859_1; } // get content and find real encoding HttpEntity entity = response.getEntity(); if (entity == null) { return null; } // get content byte[] contentBytes = EntityUtils.toByteArray(entity); if (contentBytes == null) { return null; } // found encoding if (encodingFounded) { return new String(contentBytes, encoding); } // attempt to discover encoding String rawContent = new String(contentBytes, UniversalConstants.Encoding.DEFAULT); Matcher matcher = PATTERN_HTML_CHARSET.matcher(rawContent); if (matcher.find()) { String realEncoding = matcher.group(1); if (!encoding.equalsIgnoreCase(realEncoding)) { // bad luck :( return new String(rawContent.getBytes(encoding), realEncoding); } } // not found right encoding :) return rawContent; } }; // execute CloseableHttpClient client = HttpClientHolder.getClient(); return client.execute(request, handler); }
From source file:org.sakaiproject.contentreview.urkund.util.UrkundAPIUtil.java
public static String postDocument(String baseUrl, String receiverAddress, String externalId, UrkundSubmission submission, String urkundUsername, String urkundPassword, int timeout) { String ret = null;// www . j av a2 s . c o m RequestConfig.Builder requestBuilder = RequestConfig.custom(); requestBuilder = requestBuilder.setConnectTimeout(timeout); requestBuilder = requestBuilder.setConnectionRequestTimeout(timeout); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setDefaultRequestConfig(requestBuilder.build()); try (CloseableHttpClient httpClient = builder.build()) { HttpPost httppost = new HttpPost(baseUrl + "submissions/" + receiverAddress + "/" + externalId); //------------------------------------------------------------ EntityBuilder eBuilder = EntityBuilder.create(); eBuilder.setBinary(submission.getContent()); httppost.setEntity(eBuilder.build()); //------------------------------------------------------------ if (StringUtils.isNotBlank(urkundUsername) && StringUtils.isNotBlank(urkundPassword)) { addAuthorization(httppost, urkundUsername, urkundPassword); } //------------------------------------------------------------ httppost.addHeader("Accept", "application/json"); httppost.addHeader("Content-Type", submission.getMimeType()); httppost.addHeader("Accept-Language", submission.getLanguage()); httppost.addHeader("x-urkund-filename", submission.getFilenameEncoded()); httppost.addHeader("x-urkund-submitter", submission.getSubmitterEmail()); httppost.addHeader("x-urkund-anonymous", Boolean.toString(submission.isAnon())); httppost.addHeader("x-urkund-subject", submission.getSubject()); httppost.addHeader("x-urkund-message", submission.getMessage()); //------------------------------------------------------------ HttpResponse response = httpClient.execute(httppost); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { ret = EntityUtils.toString(resEntity); EntityUtils.consume(resEntity); } } catch (IOException e) { log.error("ERROR uploading File : ", e); } return ret; }