Java 类org.apache.http.impl.client.HttpClientBuilder 实例源码

项目:scouter-pulse-aws-monitor    文件:GetMonitoringInstances.java   
private void register(RegisterModel model) throws Exception {
    String url = "http://" + properties.getScouter().getHost() + ":" + properties.getScouter().getPort() + "/register";

       String param = new Gson().toJson(model);

       HttpPost post = new HttpPost(url);
       post.addHeader("Content-Type","application/json");
       post.setEntity(new StringEntity(param));

       CloseableHttpClient client = HttpClientBuilder.create().build();

       // send the post request
       HttpResponse response = client.execute(post);

       if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            logger.info("Register message sent to [{}] for [{}].", url, model.getObject().getDisplay());
       } else {
            logger.warn("Register message sent failed. Verify below information.");
            logger.warn("[URL] : " + url);
            logger.warn("[Message] : " + param);
            logger.warn("[Reason] : " + EntityUtils.toString(response.getEntity(), "UTF-8"));
       }
}
项目:ats-framework    文件:HttpClient.java   
/**
 * Setup SSL. Pass the trusted certificates and client private key and certificate,
 * if applicable.
 *
 * @param httpClientBuilder The client builder
 * @throws HttpException
 */
private void setupSSL( HttpClientBuilder httpClientBuilder ) throws HttpException {

    try {
        SSLContextBuilder sslContextBuilder = SSLContexts.custom();

        // set trust material
        if (trustedServerCertificates != null && trustedServerCertificates.length > 0) {
            sslContextBuilder.loadTrustMaterial(convertToKeyStore(trustedServerCertificates),
                                                new TrustStrategy() {

                                                    @Override
                                                    public boolean isTrusted( X509Certificate[] chain,
                                                                              String authType ) throws CertificateException {

                                                        return checkIsTrusted(chain);
                                                    }
                                                });

        } else {
            // no trust material provided, we will trust no matter the remote party
            sslContextBuilder.loadTrustMaterial(
                                                new TrustStrategy() {
                                                    @Override
                                                    public boolean
项目:qonduit    文件:HTTPStrictTransportSecurityIT.java   
@Test
public void testHttpRequestGet() throws Exception {

    RequestConfig.Builder req = RequestConfig.custom();
    req.setConnectTimeout(5000);
    req.setConnectionRequestTimeout(5000);
    req.setRedirectsEnabled(false);
    req.setSocketTimeout(5000);
    req.setExpectContinueEnabled(false);

    HttpGet get = new HttpGet("http://127.0.0.1:54322/login");
    get.setConfig(req.build());

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setDefaultMaxPerRoute(5);

    HttpClientBuilder builder = HttpClients.custom();
    builder.disableAutomaticRetries();
    builder.disableRedirectHandling();
    builder.setConnectionTimeToLive(5, TimeUnit.SECONDS);
    builder.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);
    builder.setConnectionManager(cm);
    CloseableHttpClient client = builder.build();

    String s = client.execute(get, new ResponseHandler<String>() {

        @Override
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            assertEquals(301, response.getStatusLine().getStatusCode());
            return "success";
        }

    });
    assertEquals("success", s);

}
项目:datax    文件:RetryUtilTest.java   
@Ignore
    public void testRetryAsync3() throws Exception {
        final int TIME_OUT = 30000;
        ThreadPoolExecutor executor = RetryUtil.createThreadPoolExecutor();
        String res = RetryUtil.asyncExecuteWithRetry(new Callable<String>() {
            @Override
            public String call() throws Exception {
                RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIME_OUT)
                        .setConnectTimeout(TIME_OUT).setConnectionRequestTimeout(TIME_OUT)
                        .setStaleConnectionCheckEnabled(true).build();

                HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(10).setMaxConnPerRoute(10)
                        .setDefaultRequestConfig(requestConfig).build();

                HttpGet httpGet = new HttpGet();
                httpGet.setURI(new URI("http://0.0.0.0:8080/test"));
                httpClient.execute(httpGet);
                return OK;
            }
        }, 3, 1000L, false, 6000L, executor);
        Assert.assertEquals(res, OK);
//        Assert.assertEquals(RetryUtil.EXECUTOR.getActiveCount(), 0);
    }
项目:smarti    文件:RocketChatEndpoint.java   
public RocketChatEndpoint(
        @Value("${rocketchat.proxy.hostname:}") String proxyHostname,
        @Value("${rocketchat.proxy.port:80}") int proxyPort,
        @Value("${rocketchat.proxy.scheme:http}") String proxyScheme
) {

    httpClientBuilder = HttpClientBuilder.create()
            .setRetryHandler((exception, executionCount, context) -> executionCount < 3)
            .setConnectionBackoffStrategy(new ConnectionBackoffStrategy() {
                @Override
                public boolean shouldBackoff(Throwable t) {
                    return t instanceof IOException;
                }

                @Override
                public boolean shouldBackoff(HttpResponse resp) {
                    return false;
                }
            })
            .setUserAgent("Smarti/0.0 Rocket.Chat-Endpoint/0.1");

    if(StringUtils.isNotBlank(proxyHostname)) {
        httpClientBuilder.setProxy(new HttpHost(proxyHostname, proxyPort, proxyScheme));
    }
}
项目:mapr-music    文件:CoverArtArchiveClient.java   
private void ensureApiCalled() {

        if (!this.available) {
            return;
        }

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet getRequest = new HttpGet(RELEASE_COVERS_ENDPOINT + releaseId);
        getRequest.addHeader("accept", "application/json");

        try {

            HttpResponse response = httpClient.execute(getRequest);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                this.available = false;
                return;
            }

            this.coverArtResponse = mapper.readValue(response.getEntity().getContent(), CoverArtArchiveResponse.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
项目:nifi-atlas    文件:NiFiApiClient.java   
private String getEntity(URI url) throws IOException {
    final HttpGet get = new HttpGet(url);
    get.setConfig(requestConfig);
    get.setHeader("Accept", "application/json");

    HttpClientBuilder clientBuilder = HttpClients.custom();
    if (sslContext != null) {
        clientBuilder.setSslcontext(sslContext);
    }

    try (CloseableHttpClient httpClient = clientBuilder.build()) {

        try (CloseableHttpResponse response = httpClient.execute(get)) {

            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            if (200 != statusCode) {
                final String msg = String.format("Failed to get entity from %s, response=%d:%s",
                        get.getURI(), statusCode, statusLine.getReasonPhrase());
                throw new RuntimeException(msg);
            }
            final HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity);
        }
    }
}
项目:url-to-google-drive    文件:GoogleOauthController.java   
private User getUser(@NotNull Token token) throws IOException, URISyntaxException {

        URIBuilder builder = new URIBuilder(PROFILE_URL);
        builder.addParameter("access_token", token.getAccessToken());

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(builder.build());
        org.apache.http.HttpResponse response = httpClient.execute(httpGet);
        int statusCode = response.getStatusLine().getStatusCode();
        InputStream inputStream = response.getEntity().getContent();

        if (HttpUtilities.success(statusCode)) {
            User user = gson.fromJson(new InputStreamReader(inputStream), User.class);
            user.setToken(token);
            return user;
        }

        throw new ApiException(HttpStatus.valueOf(statusCode));
    }
项目:bubble2    文件:HttpUtils.java   
private HttpUtils(HttpRequestBase request) {
    this.request = request;

    this.clientBuilder = HttpClientBuilder.create();
    this.isHttps = request.getURI().getScheme().equalsIgnoreCase("https");
    this.config = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
    this.cookieStore = new BasicCookieStore();

    if (request instanceof HttpPost) {
        this.type = 1;
        this.builder = EntityBuilder.create().setParameters(new ArrayList<NameValuePair>());

    } else if (request instanceof HttpGet) {
        this.type = 2;
        this.uriBuilder = new URIBuilder();

    } else if (request instanceof HttpPut) {
        this.type = 3;
        this.builder = EntityBuilder.create().setParameters(new ArrayList<NameValuePair>());

    } else if (request instanceof HttpDelete) {
        this.type = 4;
        this.uriBuilder = new URIBuilder();
    }
}
项目:url-to-google-drive    文件:GoogleOauthController.java   
private Token getAccessToken(@NotNull String code) throws IOException {
    // Initialize client
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(TOKEN_URL);

    // add request parameters
    List<NameValuePair> parameters = new ArrayList<>();
    parameters.add(new BasicNameValuePair("code", code));
    parameters.add(new BasicNameValuePair("client_id", CLIENT_ID));
    parameters.add(new BasicNameValuePair("client_secret", CLIENT_SECRET));
    parameters.add(new BasicNameValuePair("redirect_uri", REDIRECT_URI));
    parameters.add(new BasicNameValuePair("grant_type", GRANT_TYPE));
    httpPost.setEntity(new UrlEncodedFormEntity(parameters));

    // send request
    org.apache.http.HttpResponse response = httpClient.execute(httpPost);
    int statusCode = response.getStatusLine().getStatusCode();
    InputStream inputStream = response.getEntity().getContent();

    if (HttpUtilities.success(statusCode))
        return gson.fromJson(new InputStreamReader(inputStream), Token.class);

    throw new ApiException(HttpStatus.valueOf(statusCode));
}
项目:pprxmtr    文件:Handler.java   
private void sendSlackImageResponse(ObjectNode json, String s3Key) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode message = mapper.createObjectNode();
        ArrayNode attachments = mapper.createArrayNode();
        ObjectNode attachment = mapper.createObjectNode();

        String emoji = json.get("text").asText();

        if (UrlValidator.getInstance().isValid(emoji)) {
            attachment.put("title_link", emoji);
            emoji = StringUtils.substringAfterLast(emoji, "/");
        }

        String username = json.get("user_name").asText();
        String responseUrl = json.get("response_url").asText();
        String slackChannelId = json.get("channel_id").asText();
        String imageUrl = String.format("https://s3.amazonaws.com/%s/%s", PROPERTIES.getProperty(S3_BUCKET_NAME), s3Key);

        message.put("response_type", "in_channel");
        message.put("channel_id", slackChannelId);
        attachment.put("title", resolveMessage("slackImageResponse", emoji, username));
        attachment.put("fallback", resolveMessage("approximated", emoji));
        attachment.put("image_url", imageUrl);
        attachments.add(attachment);
        message.set("attachments", attachments);

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost slackResponseReq = new HttpPost(responseUrl);
        slackResponseReq.setEntity(new StringEntity(mapper.writeValueAsString(message), ContentType.APPLICATION_JSON));
        HttpResponse slackResponse = client.execute(slackResponseReq);
        int status = slackResponse.getStatusLine().getStatusCode();
        LOG.info("Got {} status from Slack API after sending approximation to response url.", status);
    } catch (UnsupportedOperationException | IOException e) {
        LOG.error("Exception occured when sending Slack response", e);
    }
}
项目:pprxmtr    文件:Handler.java   
private void sendSlackTextResponse(ObjectNode json, String message) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode messageNode = mapper.createObjectNode();

        String responseUrl = json.get("response_url").asText();
        String slackChannelId = json.get("channel_id").asText();

        messageNode.put("text", message);
        messageNode.put("channel_id", slackChannelId);

        HttpClient client = HttpClientBuilder.create().build();
        HttpPost slackResponseReq = new HttpPost(responseUrl);
        slackResponseReq.setEntity(new StringEntity(mapper.writeValueAsString(messageNode), ContentType.APPLICATION_JSON));
        HttpResponse slackResponse = client.execute(slackResponseReq);
        int status = slackResponse.getStatusLine().getStatusCode();
        LOG.info("Got {} status from Slack API after sending request to response url.", status);
    } catch (UnsupportedOperationException | IOException e) {
        LOG.error("Exception occured when sending Slack response", e);
    }
}
项目:allure-java    文件:HttpClientAttachmentTest.java   
@Test
void httpClientAttachmentTest() throws IOException {
    stubFor(get(urlEqualTo("/hello"))
            .willReturn(aResponse()
                    .withBody("Hello world!")));

    final HttpClientBuilder builder = HttpClientBuilder.create()
            .addInterceptorFirst(new AllureHttpClientRequest())
            .addInterceptorLast(new AllureHttpClientResponse());
    try (CloseableHttpClient httpClient = builder.build()) {
        final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/hello", server.port()));
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            response.getStatusLine().getStatusCode();
        }
    }
}
项目:allure-java    文件:AllureHttpClientTest.java   
@SuppressWarnings("unchecked")
@Test
public void shouldCreateRequestAttachment() throws Exception {
    final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
    final AttachmentProcessor<AttachmentData> processor = mock(AttachmentProcessor.class);

    final HttpClientBuilder builder = HttpClientBuilder.create()
            .addInterceptorLast(new AllureHttpClientRequest(renderer, processor));

    try (CloseableHttpClient httpClient = builder.build()) {
        final HttpGet httpGet = new HttpGet(String.format("http://localhost:%d/hello", server.port()));
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            response.getStatusLine().getStatusCode();
        }
    }

    final ArgumentCaptor<AttachmentData> captor = ArgumentCaptor.forClass(AttachmentData.class);
    verify(processor, times(1))
            .addAttachment(captor.capture(), eq(renderer));

    assertThat(captor.getAllValues())
            .hasSize(1)
            .extracting("url")
            .containsExactly("/hello");
}
项目:groupsio-api-java    文件:BaseResource.java   
/**
 * Get a valid {@link HttpClient} to use, with a valid token.
 * 
 * @param login
 * @return
 */
private CloseableHttpClient getHttpClient(final Boolean login, final HttpUriRequest request)
{
    final HttpClientBuilder builder = HttpClientBuilder.create();
    String key;
    // if (apiClient.getApiToken() == null || apiClient.getApiToken())
    if (login)
    {
        key = apiClient.getApiKey();
    }
    else
    {
        key = apiClient.getApiToken();
    }
    key += ":";
    final byte[] credentials = Base64.encodeBase64(key.getBytes(StandardCharsets.UTF_8));
    final BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
    request.addHeader(authHeader);
    return builder.build();
}
项目:teamcity-msteams-notifier    文件:MsTeamsNotificationImpl.java   
public void setProxy(String proxyHost, Integer proxyPort, Credentials credentials) {
      this.proxyHost = proxyHost;
      this.proxyPort = proxyPort;

if (this.proxyHost.length() > 0 && !this.proxyPort.equals(0)) {
          HttpClientBuilder clientBuilder = HttpClients.custom()
              .useSystemProperties()
              .setProxy(new HttpHost(proxyHost, proxyPort, "http"));

          if (credentials != null) {
              CredentialsProvider credsProvider = new BasicCredentialsProvider();
              credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
              clientBuilder.setDefaultCredentialsProvider(credsProvider);
              Loggers.SERVER.debug("MsTeamsNotification ::using proxy credentials " + credentials.getUserPrincipal().getName());
          }

          this.client = clientBuilder.build();
}
  }
项目:mxhsd    文件:HttpFederationClient.java   
public HttpFederationClient(HomeserverState global, FederationDomainResolver resolver) {
    this.global = global;
    this.resolver = resolver;

    try {
        SocketConfig sockConf = SocketConfig.custom().setSoTimeout(30000).build();
        // FIXME properly handle SSL context by validating certificate hostname
        SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(new TrustAllStrategy()).build();
        HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        this.client = HttpClientBuilder.create()
                .disableAuthCaching()
                .disableAutomaticRetries()
                .disableCookieManagement()
                .disableRedirectHandling()
                .setDefaultSocketConfig(sockConf)
                .setSSLSocketFactory(sslSocketFactory)
                .setUserAgent(global.getAppName() + "/" + global.getAppVersion())
                .build();
    } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) {
        throw new RuntimeException(e);
    }
}
项目:desafio-pagarme    文件:Client.java   
/**
 * Creates the security rest template.
 *
 * @throws KeyManagementException the key management exception
 * @throws NoSuchAlgorithmException the no such algorithm exception
 * @throws KeyStoreException the key store exception
 */
private void createSecurityRestTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException{
    TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
            .loadTrustMaterial(null, acceptingTrustStrategy)
            .build();
    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

    HttpComponentsClientHttpRequestFactory requestFactory =
            new HttpComponentsClientHttpRequestFactory();

     HttpClient httpClient = HttpClientBuilder.create()
               .disableCookieManagement()
               .useSystemProperties()
               .setSSLSocketFactory(csf)
               .build();
    requestFactory.setHttpClient(httpClient);
    this.restTemplate = new RestTemplate(requestFactory);
}
项目:crnk-framework    文件:HttpClientAdapter.java   
private HttpClientBuilder createBuilder() {
    // brave enforces this, hopefully can be removed again eventually

    HttpClientBuilder builder = null;
    for (HttpClientAdapterListener listener : listeners) {
        if (listener instanceof HttpClientBuilderFactory) {
            PreconditionUtil
                    .assertNull("only one module can contribute a HttpClientBuilder with HttpClientBuilderFactory", builder);
            builder = ((HttpClientBuilderFactory) listener).createBuilder();
        }
    }

    if (builder != null) {
        return builder;
    }
    else {
        return HttpClients.custom();
    }

}
项目:outcomes    文件:RfiController.java   
@RequestMapping(value = "/rfi/weChat", method = RequestMethod.POST)
public void testPost(@RequestParam(value = "code", required = false) String code) throws IOException {
    System.out.println(code);
    System.out.println("====================================");

    String url = "https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https%3A%2F%2Fpassport.yhd.com%2Fwechat%2Fcallback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect";
    HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();
    CloseableHttpClient client = builder.build();
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    InputStream inputStream = response.getEntity().getContent();
    byte [] tt = new byte[1024];
    while (inputStream.read(tt) != -1) {
        System.out.println(new String(tt,"utf-8"));
    }
}
项目:stock-api-sdk    文件:ApiUtils.java   
/**
 * Initializes Http Client.
 *
 * @return Instance of HttpClient
 */
private static HttpClient initialize() {
    PoolingHttpClientConnectionManager connManager
            = new PoolingHttpClientConnectionManager();
    RequestConfig config = RequestConfig.custom()
            .setConnectionRequestTimeout(TIME_OUT)
            .setSocketTimeout(TIME_OUT).build();
    HttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(connManager).disableRedirectHandling()
            .setDefaultRequestConfig(config).build();
    return httpClient;
}
项目:security-karate    文件:LoginExecutor.java   
/**
 * Perform a http login and return the acquired session ID.
 *
 * @param credentials the credentials to login with
 * @param csrfToken   the csrfToken form the login page
 *
 * @param forwardedForHeader
 * @return the sessionId if login was successful
 *
 * @throws IOException
 */
private Optional<String> login(Credentials credentials, String csrfToken, Header forwardedForHeader) throws IOException {
    Optional<String> sessionId;
    CloseableHttpClient httpclient = HttpClientBuilder.create()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .build();

    try {
        HttpPost httpPost = new HttpPost(configuration.getLoginUrl());
        httpPost.setHeader(forwardedForHeader);

        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("username", credentials.getUsername()));
        nvps.add(new BasicNameValuePair("password", credentials.getPassword()));
        nvps.add(new BasicNameValuePair("_csrf", csrfToken));

        String initialSession = getCurrentSession(context);

        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        CloseableHttpResponse response2 = httpclient.execute(httpPost, context);

        try {
            logger.debug(response2.getStatusLine().toString());
            sessionId = extractSessionId(context);
            if(initialSession != null && initialSession.equals(sessionId.orElse("nothing"))){
                return Optional.empty();
            }
        } finally {
            response2.close();
        }

    } finally {
        httpclient.close();
    }
    return sessionId;
}
项目:Reer    文件:HttpClientConfigurer.java   
public void configure(HttpClientBuilder builder) {
    SystemDefaultCredentialsProvider credentialsProvider = new SystemDefaultCredentialsProvider();
    configureSslSocketConnectionFactory(builder, httpSettings.getSslContextFactory());
    configureAuthSchemeRegistry(builder);
    configureCredentials(builder, credentialsProvider, httpSettings.getAuthenticationSettings());
    configureProxy(builder, credentialsProvider, httpSettings);
    configureUserAgent(builder);
    builder.setDefaultCredentialsProvider(credentialsProvider);
}
项目:Reer    文件:HttpClientConfigurer.java   
private void configureAuthSchemeRegistry(HttpClientBuilder builder) {
    builder.setDefaultAuthSchemeRegistry(RegistryBuilder.<AuthSchemeProvider>create()
        .register(AuthSchemes.BASIC, new BasicSchemeFactory())
        .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
        .register(AuthSchemes.NTLM, new NTLMSchemeFactory())
        .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
        .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory())
        .build()
    );
}
项目:Reer    文件:HttpClientConfigurer.java   
private void configureProxy(HttpClientBuilder builder, CredentialsProvider credentialsProvider, HttpSettings httpSettings) {
    HttpProxySettings.HttpProxy httpProxy = httpSettings.getProxySettings().getProxy();
    HttpProxySettings.HttpProxy httpsProxy = httpSettings.getSecureProxySettings().getProxy();

    for (HttpProxySettings.HttpProxy proxy : Lists.newArrayList(httpProxy, httpsProxy)) {
        if (proxy != null) {
            if (proxy.credentials != null) {
                useCredentials(credentialsProvider, proxy.host, proxy.port, Collections.singleton(new AllSchemesAuthentication(proxy.credentials)));
            }
        }
    }
    builder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
}
项目:logistimo-web-service    文件:RestConfig.java   
private static ClientHttpRequestFactory getClientHttpRequestFactory() {
  int timeout = ConfigUtil.getInt("api.client.timeout", 5000);
  RequestConfig config = RequestConfig.custom()
      .setConnectTimeout(timeout)
      .setConnectionRequestTimeout(timeout)
      .setSocketTimeout(timeout)
      .build();
  CloseableHttpClient client = HttpClientBuilder
      .create()
      .setDefaultRequestConfig(config)
      .build();
  return new HttpComponentsClientHttpRequestFactory(client);
}
项目:Patterdale    文件:PatterdaleTest.java   
@Test
public void scrapesOracleDatabaseMetricsOnRequest() throws Exception {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response = httpClient.execute(new HttpGet("http://localhost:7001/metrics"));

    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);

    assertThat(responseBody(response)).matches(Pattern.compile(
            "database_up\\{database=\"bobsDatabase\",query=\"SELECT 1 FROM DUAL\"} 1.0\n" +
                    "database_up\\{database=\"alicesDatabase\",query=\"SELECT 1 FROM DUAL\"} 1.0\n" +
                    ".*"
            , Pattern.DOTALL)
    );
}
项目:OnionHarvester    文件:OH.java   
public static synchronized Object[] getNewOnions() {
    Vector<Object> out = new Vector<>();

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(URLGenerate);

    // add request header
    request.addHeader("User-Agent", "OnionHarvester - Java Client");
    try {
        HttpResponse response = client.execute(request);

        if (response.getStatusLine().getStatusCode() != 200) {
            out.add(false);
            return out.toArray();
        }

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        String temp = result.toString();
        jobj = new JSONObject(temp);
        jobj.getJSONArray("ports").iterator().forEachRemaining(o -> {
            getPorts().add(Integer.valueOf((String) o));
        });
        out.add(true);
        out.add(jobj.getString("start"));
        out.add(jobj.getString("end"));
        out.add(jobj.getString("id"));
    } catch (Exception ex) {
        out.add(false);
    } finally {
        return out.toArray();
    }
}
项目:camunda-task-dispatcher    文件:ExternalTaskRestServiceImpl.java   
@PostConstruct
public void init() {
    objectMapper = new ObjectMapper();
    final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (StringUtils.hasText(camundaUser)) {
        final BasicCredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(camundaUser, camundaPass));
        clientBuilder.setDefaultCredentialsProvider(provider);
    }
    httpClient = clientBuilder.build();
}
项目:sling-org-apache-sling-starter    文件:SmokeIT.java   
private CloseableHttpClient newClient() {

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
        credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds);

        return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
    }
项目:dcits-report    文件:Start.java   
/**
 * 每次报工结束,关闭该client,下次报工启用新的client
 */
private static void getHttpClient() {
    HttpClientUtils.closeQuietly(client);
    // Locale.setDefault(new Locale("zh","CN"));
    HttpClientBuilder builder = HttpClientBuilder.create();
    client = builder.build();
}
项目:spring-cloud-samples    文件:ApplicationTests.java   
public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClientBuilder builder = HttpClientBuilder.create();
//      HttpPost post = new HttpPost("http://localhost:8088/refresh");
        HttpPost post = new HttpPost("http://localhost:8888/bus/refresh");
        CloseableHttpResponse response = builder.build().execute(post);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
项目:TradingRobot    文件:Trading.java   
private int closeExistingTrade(String buyOrSell) {
    Trade t = getTrade();
    long existingTradeId;
    if (t != null) {
        if (t.getBuyOrSell().contentEquals(buyOrSell)) return 1;
        existingTradeId = t.getId();
    }
    else {
        //No trade is open
        return 0;
    }

    HttpClientBuilder hcb = HttpClientBuilder.create();
    HttpClient client = hcb.build();
    HttpPut request = new HttpPut(RequestURI.baseURL+"/v3/accounts/"+RequestURI.accountId+"/trades/"+existingTradeId+"/close");
    request.addHeader(RequestURI.headerTitle,RequestURI.accessToken);
    System.out.println(request.getURI());
    try {
        HttpResponse response = client.execute(request);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        JSONObject resultJson = new JSONObject(result.toString());
        System.out.println(resultJson);
        return response.getStatusLine().getStatusCode();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 0;
    }

}
项目:datarouter    文件:DatarouterHttpClientBuilder.java   
public DatarouterHttpClientBuilder(){
    this.retryHandler = new DatarouterHttpRetryHandler();
    this.timeoutMs = DEFAULT_TIMEOUT_MS;
    this.maxTotalConnections = DEFAULT_MAX_TOTAL_CONNECTION;
    this.maxConnectionsPerRoute = DEFAULT_MAX_CONNECTION_PER_ROUTE;
    this.httpClientBuilder = HttpClientBuilder.create()
            .setRetryHandler(retryHandler)
            .setRedirectStrategy(new LaxRedirectStrategy());
}
项目:kafka-connect-marklogic    文件:MarkLogicWriter.java   
public MarkLogicWriter(final Map<String, String> config){

    connectionUrl = config.get(MarkLogicSinkConfig.CONNECTION_URL);
    user = config.get(MarkLogicSinkConfig.CONNECTION_USER);
    password = config.get(MarkLogicSinkConfig.CONNECTION_PASSWORD);

    requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5 * 1000).build();
    localContext = HttpClientContext.create();
    httpClient = HttpClientBuilder.create().build();
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
    localContext.setCredentialsProvider(credentialsProvider);
    localContext.setRequestConfig(requestConfig);
}
项目:ARCLib    文件:SpnegoHelper.java   
public static void addSpnego(HttpClientBuilder clientBuilder) {
    //Add spnego http header processor
    Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create()
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
    clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    //There has to be at least this dummy credentials provider or apache http client gives up
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(null, -1, null), new NullCredentials());
    clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
项目:qonduit    文件:HttpClient.java   
public static CloseableHttpClient get(SSLContext ssl, CookieStore cookieStore, boolean hostVerificationEnabled) {
    RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();

    HttpClientBuilder builder = HttpClients.custom().setSSLContext(ssl).setDefaultCookieStore(cookieStore)
            .setDefaultRequestConfig(defaultRequestConfig);
    if (hostVerificationEnabled) {
        builder.setSSLHostnameVerifier(new DefaultHostnameVerifier());
    } else {
        builder.setSSLHostnameVerifier(new NoopHostnameVerifier());
    }
    return builder.build();
}
项目:datax    文件:HttpClientUtil.java   
private void initApacheHttpClient() {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_TIMEOUT_INMILLIONSECONDS)
            .setConnectTimeout(HTTP_TIMEOUT_INMILLIONSECONDS).setConnectionRequestTimeout(HTTP_TIMEOUT_INMILLIONSECONDS)
            .setStaleConnectionCheckEnabled(true).build();

      if(null == provider) {
          httpClient = HttpClientBuilder.create().setMaxConnTotal(POOL_SIZE).setMaxConnPerRoute(POOL_SIZE)
                  .setDefaultRequestConfig(requestConfig).build();
      } else {
          httpClient = HttpClientBuilder.create().setMaxConnTotal(POOL_SIZE).setMaxConnPerRoute(POOL_SIZE)
                  .setDefaultRequestConfig(requestConfig).setDefaultCredentialsProvider(provider).build();
      }
}
项目:RoboInsta    文件:InstagramContext.java   
private CloseableHttpClient createHttpClient() {
    final RequestConfig requestConfig = RequestConfig.custom()
        .setCookieSpec(CookieSpecs.DEFAULT)
        .setAuthenticationEnabled(true)
        .setRedirectsEnabled(true)
        .build();
    return HttpClientBuilder.create()
        .setDefaultCookieStore(cookieStore)
        .setDefaultRequestConfig(requestConfig)
        .build();
}
项目:webBee    文件:HttpClientPool.java   
/**
 * 根据配置自动生成需要的HTTP配置
 * @param setting
 * @return
 */
public HttpClientBuilder generateClient(Setting setting, HttpRequestBase httpMethod){
    RequestConfig requestConfig = RequestConfig.custom().build();
    HttpClientBuilder httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig);
    generateHeaders(httpMethod,setting);
    generateCookies(httpClient,setting);
    return httpClient;
}