Java 类org.apache.http.impl.client.HttpClients 实例源码
项目:outcomes
文件:TestHttpCore.java
@Test
public void client() throws URISyntaxException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.google.com")
.setPath("/search")
.setParameter("q", "httpclient")
.setParameter("btnG", "Google Search")
.setParameter("aq", "f")
.setParameter("oq", "")
.build();
HttpGet httpget = new HttpGet(uri);
CloseableHttpResponse response = httpclient.execute(httpget);
}
项目:Burp-Hunter
文件:HunterRequest.java
public String notifyHunter(byte[] content) throws IOException {
try {
String request = new String(content);
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
HttpClient httpclient = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpPost httpPost = new HttpPost("https://api"+hunterDomain.substring(hunterDomain.indexOf("."))+"/api/record_injection");
String json = "{\"request\": \""+request.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r\n", "\\n")+"\", \"owner_correlation_key\": \""+hunterKey+"\", \"injection_key\": \""+injectKey+"\"}";
StringEntity entity = new StringEntity(json);
entity.setContentType("applicaiton/json");
httpPost.setEntity(entity);
HttpResponse response = httpclient.execute(httpPost);
String responseString = new BasicResponseHandler().handleResponse(response);
return responseString;
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
Logger.getLogger(HunterRequest.class.getName()).log(Level.SEVERE, null, ex);
}
return "Error Notifying Probe Server!";
}
项目:pyroclast-java
文件:PyroclastDeploymentClient.java
public ReadAggregatesResult readAggregates() throws IOException, PyroclastAPIException {
ensureBaseAttributes();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = String.format("%s/%s/aggregates", this.buildEndpoint(), this.deploymentId);
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Authorization", this.readApiKey);
httpGet.addHeader("Content-type", FORMAT);
ReadAggregatesResult result;
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
ResponseParser<ReadAggregatesResult> parser = new ReadAggregatesParser();
result = parser.parseResponse(response, MAPPER);
}
return result;
}
}
项目:websocket-poc
文件:App.java
@Bean
public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
return new RestTemplate(requestFactory);
}
项目:devops-cstack
文件:RestUtils.java
public Map<String, String> connect(String url, Map<String, Object> parameters) throws ManagerResponseException {
Map<String, String> response = new HashMap<String, String>();
CloseableHttpClient httpclient = HttpClients.createDefault();
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("j_username", (String) parameters.get("login")));
nvps.add(new BasicNameValuePair("j_password", (String) parameters.get("password")));
localContext = HttpClientContext.create();
localContext.setCookieStore(new BasicCookieStore());
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext);
ResponseHandler<String> handler = new CustomResponseErrorHandler();
String body = handler.handleResponse(httpResponse);
response.put(BODY, body);
httpResponse.close();
} catch (Exception e) {
authentificationUtils.getMap().clear();
throw new ManagerResponseException(e.getMessage(), e);
}
return response;
}
项目:Snow-Globe
文件:CallUtility.java
/**
* Builds a custom Http client with custom DNS resolution, disabling persistent cookie stores and with custom
* timeout values.
*
* @return An http client to be used to execute test requests to nginx.
*/
static CloseableHttpClient buildHttpClient() {
return HttpClients.custom()
.setConnectionManager(buildConnectionManager())
.setDefaultRequestConfig(RequestConfig.custom()
// Waiting for a connection from connection manager
.setConnectionRequestTimeout(100)
// Waiting for connection to establish
.setConnectTimeout(100)
.setExpectContinueEnabled(false)
// Waiting for data
.setSocketTimeout(200)
// Do not allow cookies to be stored between calls.
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build())
.setRetryHandler(buildRetryHandler())
.disableRedirectHandling().build();
}
项目:CTQS
文件:test.java
@Test
public void test1(){
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(
"http://hncjzf.bibibi.net/module/getcareers?start=0&count=50&keyword=&address=&professionals=&career_type=&type=inner&day=2017-11-08");
HttpResponse response = httpClient.execute(httpGet);
JSONObject object = JSON.parseObject(EntityUtils.toString(response.getEntity()));
JSONArray data = object.getJSONArray("data");
List<HUELList> list = JSON.parseArray(data.toJSONString(), HUELList.class);
//HUELList list1 = list.get(0);
huelService.saveList(list);
}catch (IOException e){
}
}
项目:seldon-core
文件:InternalPredictionService.java
@Autowired
public InternalPredictionService(AppProperties appProperties){
this.appProperties = appProperties;
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(150);
connectionManager.setDefaultMaxPerRoute(150);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(DEFAULT_REQ_TIMEOUT)
.setConnectTimeout(DEFAULT_CON_TIMEOUT)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT).build();
httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(new HttpRetryHandler())
.build();
}
项目:cloud-language-servers-container
文件:LanguageServerWSEndPointTest.java
@Test
public void onOpenLsp_timeout() {
// HttpClient for WSSynchronize notification
mockStatic(HttpClients.class);
expect(HttpClients.createSystem()).andReturn(dummyHttpClient);
replayAll();
Map<String, List<String>> reqParam = new HashMap<>();
reqParam.put("lsp_timeout", Arrays.asList("100"));
Session testSessionTO = Mockito.spy(Session.class);
Mockito.when(testSessionTO.getRequestParameterMap()).thenReturn(reqParam);
Mockito.when(testSessionTO.getId()).thenReturn("0");
Mockito.when(testSessionTO.getNegotiatedSubprotocol()).thenReturn("access_token");
Mockito.when(testSessionTO.getBasicRemote()).thenReturn(remoteEndpointMock);
doReturn("/").when(lspProcessMock).getProjPath();
cut.onOpen("testWS", "aLang", testSessionTO, endpointConfig);
Mockito.verify(testSessionTO, times(1)).setMaxIdleTimeout(100L);
assertEquals(READY_MESSAGE, readyMessage);
assertEquals("Wrong registration data ", "/:aLang=/testWS/aLang", TestUtils.getInternalState(dummyHttpClient, "regData"));
}
项目:cos-java-sdk-v5
文件:DefaultCosHttpClient.java
private void initHttpClient() {
this.connectionManager.setMaxTotal(this.clientConfig.getMaxConnectionsCount());
this.connectionManager.setDefaultMaxPerRoute(this.clientConfig.getMaxConnectionsCount());
this.connectionManager.setValidateAfterInactivity(1);
HttpClientBuilder httpClientBuilder =
HttpClients.custom().setConnectionManager(connectionManager);
if (this.clientConfig.getHttpProxyIp() != null
&& this.clientConfig.getHttpProxyPort() != 0) {
HttpHost proxy = new HttpHost(this.clientConfig.getHttpProxyIp(),
this.clientConfig.getHttpProxyPort());
httpClientBuilder.setProxy(proxy);
}
this.httpClient = httpClientBuilder.build();
this.requestConfig =
RequestConfig.custom()
.setConnectionRequestTimeout(
this.clientConfig.getConnectionRequestTimeout())
.setConnectTimeout(this.clientConfig.getConnectionTimeout())
.setSocketTimeout(this.clientConfig.getSocketTimeout()).build();
this.idleConnectionMonitor = new IdleConnectionMonitorThread(this.connectionManager);
this.idleConnectionMonitor.start();
}
项目:pyroclast-java
文件:PyroclastDeploymentClient.java
public ReadAggregateGroupResult readAggregateGroup(String aggregateName, String groupName) throws IOException, PyroclastAPIException {
ensureBaseAttributes();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = String.format("%s/%s/aggregates/%s/group/%s",
this.buildEndpoint(), this.deploymentId, aggregateName, groupName);
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Authorization", this.readApiKey);
httpGet.addHeader("Content-type", FORMAT);
ReadAggregateGroupResult result;
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
ResponseParser<ReadAggregateGroupResult> parser = new ReadAggregateGroupParser();
result = parser.parseResponse(response, MAPPER);
}
return result;
}
}
项目:spring-credhub
文件:ClientHttpRequestFactoryFactory.java
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options)
throws GeneralSecurityException, IOException {
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLContext(SSLContext.getDefault())
.useSystemProperties();
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setAuthenticationEnabled(true);
if (options.getConnectionTimeout() != null) {
requestConfigBuilder.setConnectTimeout(options.getConnectionTimeout());
}
if (options.getReadTimeout() != null) {
requestConfigBuilder.setSocketTimeout(options.getReadTimeout());
}
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
}
项目: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);
}
}
}
项目:devops-cstack
文件:RestUtils.java
/**
* sendPostCommand
*
* @param url
* @param credentials
* @param entity
* @return
* @throws ClientProtocolException
*/
public Map<String, Object> sendPostCommand(String url, Map<String, Object> credentials, String entity)
throws ManagerResponseException {
Map<String, Object> response = new HashMap<String, Object>();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
try {
StringEntity stringEntity = new StringEntity(entity);
httpPost.setEntity(stringEntity);
CloseableHttpResponse httpResponse = httpclient.execute(httpPost, localContext);
ResponseHandler<String> handler = new CustomResponseErrorHandler();
String body = handler.handleResponse(httpResponse);
response.put(BODY, body);
httpResponse.close();
} catch (Exception e) {
throw new ManagerResponseException(e.getMessage(), e);
}
return response;
}
项目:athena
文件:RestSBControllerImpl.java
private CloseableHttpClient getApacheSslBypassClient() throws NoSuchAlgorithmException,
KeyManagementException, KeyStoreException {
return HttpClients.custom().
setHostnameVerifier(new AllowAllHostnameVerifier()).
setSslcontext(new SSLContextBuilder()
.loadTrustMaterial(null, (arg0, arg1) -> true)
.build()).build();
}
项目:SensorThingsManager
文件:AuthBasic.java
@Override
public void setAuth(SensorThingsService service) {
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
URL url = service.getEndpoint().toURL();
credsProvider.setCredentials(
new AuthScope(url.getHost(), url.getPort()),
new UsernamePasswordCredentials(editorUsername.getValue(), editorPassword.getValue()));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
service.setClient(httpclient);
} catch (MalformedURLException ex) {
LOGGER.error("Failed to initialise basic auth.", ex);
}
}
项目:simple-hostel-management
文件:Application.java
private static void askForUpdate() {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(SpringConfiguration.UPDATE_SERVER_ADDRESS);
try (CloseableHttpResponse response1 = httpclient.execute(httpGet)) {
//System.out.println(Arrays.asList(response1.getAllHeaders()));
HttpEntity entity = response1.getEntity();
try (BufferedReader bis = new BufferedReader(new InputStreamReader(entity.getContent()))) {
String line = null;
String content = "";
while ((line = bis.readLine()) != null) {
content += line;
}
logger.warn("Update message: " + content);
}
EntityUtils.consume(entity);
} catch (IOException e) {
logger.error("Unable to reach update server", e);
}
}
项目:zxbangban
文件:WXAutoUtil.java
public static JSONObject doGetJson(String url) throws IOException {
JSONObject jsonObject = null;
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet);
HttpEntity httpEntity = closeableHttpResponse.getEntity();
if (httpEntity != null) {
String result = EntityUtils.toString(httpEntity, "UTF-8");
jsonObject = JSONObject.fromObject(result);
}
httpGet.releaseConnection();
closeableHttpClient.close();
return jsonObject;
}
项目:JInsight
文件:DefaultHttpClientInstrumentationTest.java
@Override
protected HttpClient createClient() {
HttpClient httpclient;
httpclient = HttpClients.createDefault();
/*
httpclient = HttpClients.createMinimal();
httpclient = HttpClientBuilder.create().build();
httpclient = new DefaultHttpClient();
httpclient = new DecompressingHttpClient(new DefaultHttpClient())
HttpRequestExecutor executor = new HttpRequestExecutor();
executor.preProcess(request, processor, context);
HttpResponse response = executor.execute(request, connection, context);
executor.postProcess(response, processor, context);
*/
return httpclient;
}
项目:ts-benchmark
文件:HttpPoolManager.java
public static CloseableHttpClient getHttpClient() {
// CloseableHttpClient httpClient = HttpClients.custom()
// .setConnectionManager(cm)
// .build();
// return httpClient;
if(httpClient==null){
synchronized (HttpPoolManager.class) {
if(httpClient==null){
// ConnectionConfig config = ConnectionConfig.custom()
// .setBufferSize(4128)
// .build();
httpClient = HttpClients.custom()
.setConnectionManager(cm)
// .setDefaultConnectionConfig(config)
.build();
}
}
}
return httpClient;
}
项目:cloud-ariba-partner-flow-extension-ext
文件:OpenApisEndpoint.java
/**
* Performs HTTP Get request with OAuth authentication for the endpoint with
* the given path with the given HTTP headers.
*
* @param path
* the path to be called.
* @param headers
* map with HTTP header names and values to be included in the
* request.
* @return the CloseableHttpResponse object.
* @throws ClientProtocolException
* @throws IOException
*/
CloseableHttpResponse executeHttpGet(String path, Map<String, String> headers)
throws ClientProtocolException, IOException {
logger.debug(DEBUG_EXECUTING_HTTP_GET_FOR, baseUri, path);
HttpGet httpGet = createHttpGet(baseUri + path);
for (String header : headers.keySet()) {
httpGet.addHeader(header, headers.get(header));
}
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);
logger.debug(DEBUG_EXECUTED_HTTP_GET_FOR, baseUri, path);
return response;
}
项目:ares
文件:BotControllerInteractionsTest.java
@Test
public void test_submitReport_remote() {
Controller controller = new Controller(
"http://localhost:3000/init",
"http://localhost:3000/command",
"http://localhost:3000/report");
CloseableHttpClient client = HttpClients.createDefault();
Report expected = new SimpleReport();
expected.put("foo", "bar");
try {
BotControllerInteractions.submitReport(controller, expected, client);
client.close();
} catch (IOException exc) {
LOGGER.error(exc.getMessage());
}
}
项目:BUbiNG
文件:FetchDataTest.java
public static CloseableHttpClient getHttpClient(final HttpHost proxy, final boolean redirects, final CookieStore cookieStore) {
final Builder builder = RequestConfig.custom()
.setRedirectsEnabled(redirects)
.setMaxRedirects(5);
if (proxy != null) builder.setProxy(proxy);
final RequestConfig requestConfig = builder.build();
return HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore)
.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
.build();
}
项目:activiti-analytics-spring-boot
文件:ElasticHTTPClient.java
public ElasticHTTPClient() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(200);
cm.setMaxTotal(200);
httpClient = HttpClients.custom().setConnectionManager(cm).build();
}
项目:xm-uaa
文件:TemplateUtil.java
public static void disableSSL(RestTemplate restTemplate) {
try {
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, (X509Certificate[] chain, String authType) -> true)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory));
} catch (Exception e) {
log.error("Exception occurred while creating http factory, error={}", e.getMessage(), e);
}
}
项目:csap-core
文件:NagiosIntegration.java
/**
* WARNING!!! disabling is not a good idea.
*
* Support hardcode for ps team
*
* Their server is pretty busted. -
* http://stackoverflow.com/questions/7615645
* /ssl-handshake-alert-unrecognized-name-error-since-upgrade-to-java-1-7-0
* -- -Djsse.enableSNIExtension=false very ugly workaround
*
* @param restTemplate
* @throws Exception
*/
public static HttpComponentsClientHttpRequestFactory getFactoryDisabledSslChecks(
int connectTimeoutMs, int readTimeoutMs) throws Exception {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
// builder.loadTrustMaterial(null, new TrustStrategy() {
//
// @Override
// public boolean isTrusted(X509Certificate[] chain, String authType)
// throws CertificateException {
//
// return true;
// }
// });
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build(), new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
// factory.getHttpClient().getConnectionManager().getSchemeRegistry().register(scheme);
factory.setConnectTimeout(connectTimeoutMs);
factory.setReadTimeout(readTimeoutMs);
// restTemplate.setRequestFactory(factory);
return factory;
}
项目:CTQS
文件:HttpClientUtil.java
/**
* @Author: GaoYongjian
* @Description: setConnectTimeout 从连接池获取连接超时时间
* setConnectTimeout 连接服务器的时间
* setSocketTimeout 服务器返回数据的时间
* @Date: Created in 10:45 2017-12-29
*/
public static CloseableHttpClient getHttpClient() {
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(50);
client = HttpClients.custom().setConnectionManager(cm)
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(2000).setConnectTimeout(2000).setSocketTimeout(2000)
.build())
.build();
return client;
}
项目:uberscriptquery
文件:HttpUtils.java
public static HttpResponse get(String url, Map<String, String> header) {
int statusCode = 0;
String responseBody = "";
try {
logger.info(String.format("Getting url: %s", url));
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(url);
if (header != null && !header.isEmpty()) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue());
}
}
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
statusCode = httpResponse.getStatusLine().getStatusCode();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()))) {
responseBody = IOUtils.toString(reader);
logger.info(String.format("Finished getting url: %s, Http status: %s, Response body: %s",
url, statusCode, truncateLogString(responseBody)));
}
}
}
return new HttpResponse(statusCode, responseBody);
} catch (Exception ex) {
String msg = String.format("Failed getting url: %s", url);
logger.warn(msg, ex);
throw new RuntimeException(msg, ex);
}
}
项目:Java-9-Cookbook
文件:ApacheHttpClientDemo.java
public static void main(String [] args) throws Exception{
CloseableHttpClient client = HttpClients.createDefault();
try{
HttpGet request = new HttpGet("http://httpbin.org/get");
CloseableHttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Status code: " + statusCode);
System.out.println("Response Body: " + responseBody);
}finally{
client.close();
}
}
项目:ProxyPool
文件:HttpManager.java
/**
* 获取Http客户端连接对象
* @param timeOut 超时时间
* @param proxy 代理
* @param cookie Cookie
* @return Http客户端连接对象
*/
public CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) {
// 创建Http请求配置参数
RequestConfig.Builder builder = RequestConfig.custom()
// 获取连接超时时间
.setConnectionRequestTimeout(timeOut)
// 请求超时时间
.setConnectTimeout(timeOut)
// 响应超时时间
.setSocketTimeout(timeOut)
.setCookieSpec(CookieSpecs.STANDARD);
if (proxy!=null) {
builder.setProxy(proxy);
}
RequestConfig requestConfig = builder.build();
// 创建httpClient
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder
// 把请求相关的超时信息设置到连接客户端
.setDefaultRequestConfig(requestConfig)
// 把请求重试设置到连接客户端
.setRetryHandler(new RetryHandler())
// 配置连接池管理对象
.setConnectionManager(connManager);
if (cookie!=null) {
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
httpClientBuilder.setDefaultCookieStore(cookieStore);
}
return httpClientBuilder.build();
}
项目:Auts_Assert_manager
文件:HttpsRequest.java
private void init() throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException,
KeyManagementException {
/*
* KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream
* instream = new FileInputStream(new
* File(Configure.getCertLocalPath()));// 加载本地的证书进行https加密传输 try {
* keyStore.load(instream, Configure.getCertPassword().toCharArray());//
* 设置证书密码 } catch (CertificateException e) { e.printStackTrace(); }
* catch (NoSuchAlgorithmException e) { e.printStackTrace(); } finally {
* instream.close(); }
*
* // Trust own CA and all self-signed certs SSLContext sslcontext =
* SSLContexts.custom() .loadKeyMaterial(keyStore,
* Configure.getCertPassword().toCharArray()).build(); // Allow TLSv1
* protocol only SSLConnectionSocketFactory sslsf = new
* SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
* null,
* SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
*
* httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
*/
httpClient = HttpClients.custom().build();
// 根据默认超时限制初始化requestConfig
requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout)
.build();
hasInit = true;
}
项目:BiliInfoCrawler
文件:Test.java
public String getJson(int w, int pagenum) throws IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpGet req = new HttpGet(jsonURL1 + tids[w] + jsonURL2 + pagenum);
req.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
req.addHeader("Accept-Encoding", "gzip,deflate");
req.addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
req.addHeader("Content-Type", "text/html; charset=UTF-8");
req.addHeader("User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse resp = (CloseableHttpResponse) httpClient.execute(req);
HttpEntity repEntity = resp.getEntity();
String content = "[" + EntityUtils.toString(repEntity) + "]";
return content;
}
项目:spring-boot-readiness
文件:RestTemplateConfiguration.java
@Bean("httpClient")
@Profile("insecure")
@SneakyThrows
public HttpClient insecureHttpClient(@Value("${spring.application.name}") String userAgent) {
// http://stackoverflow.com/a/41618092/1393467
TrustStrategy trustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, trustStrategy).build();
return configure(HttpClients.custom(), userAgent)
.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext))
.build();
}
项目:InComb
文件:TxtContentReader.java
/**
* Returns the content read form content-source.
* @return read {@link Content}
*/
@Override
public Content[] read(final FetchHistory lastFetch) {
final CloseableHttpClient httpClient = HttpClients.createDefault();
final HttpGet httpget = new HttpGet(contentSource.getUrl());
final HttpContext context = new BasicHttpContext();
CloseableHttpResponse response = null;
String stringRead = null;
try {
try {
LOGGER.info("Loading uri: " + httpget.getURI());
response = httpClient.execute(httpget, context);
final HttpEntity entity = response.getEntity();
if (entity != null) {
stringRead = IOUtils.toString(entity.getContent());
LOGGER.info("Read {} bytes from: {}", stringRead.getBytes().length, httpget.getURI());
}
} finally {
CloseUtil.close(response);
CloseUtil.close(httpClient);
}
} catch (final Exception e) {
LOGGER.warn("Error occurred while reading text document: " + contentSource.getUrl());
}
return new Content[] { createContentObject(stringRead) };
}
项目:ZhihuQuestionsSpider
文件:Downloader.java
public Downloader(){
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
this.httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setSSLSocketFactory(createSSLConnSocketFactory())
.build();
}
项目:snowflake
文件:HttpClientUtil.java
/**
* 初始化httpclient对象
*/
private static void buildHttpClient() {
RequestConfig globalConfig =
RequestConfig.custom().setConnectTimeout(5000)
.setSocketTimeout(5000).build();
CloseableHttpClient httpclient =
HttpClients.custom().setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.setDefaultRequestConfig(globalConfig).build();
HttpClientUtil.httpclient = httpclient;
}
项目:aem-epic-tool
文件:ConnectionManager.java
public CloseableHttpClient getAuthenticatedClient(CredentialsProvider creds, HttpContext defaultContext) {
CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultCredentialsProvider(creds)
.build();
if (defaultContext != null) {
this.defaultContext = defaultContext;
}
return httpclient;
}
项目:pay
文件:HttpsRequest.java
private void init() throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(config.getCertLocalPath()));//加载本地的证书进行https加密传输
try {
keyStore.load(instream,config.getCertPassword().toCharArray());//设置证书密码
} catch (Exception e) {
e.printStackTrace();
} finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom()
.loadKeyMaterial(keyStore, config.getCertPassword().toCharArray())
.build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpClient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
//根据默认超时限制初始化requestConfig
requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
hasInit = true;
}
项目:xm-ms-entity
文件:TemplateUtil.java
public static void disableSSL(RestTemplate restTemplate) {
try {
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, (X509Certificate[] chain, String authType) -> true)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory));
} catch (Exception e) {
log.error("Exception occurred while creating http factory, error={}", e.getMessage(), e);
}
}