/** * get a redirect for an url: this method shall be called if it is expected that a url * is redirected to another url. This method then discovers the redirect. * @param urlstring * @param useAuthentication * @return the redirect url for the given urlstring * @throws IOException if the url is not redirected */ public static String getRedirect(String urlstring) throws IOException { HttpGet get = new HttpGet(urlstring); get.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); get.setHeader("User-Agent", ClientIdentification.getAgent(ClientIdentification.yacyInternetCrawlerAgentName).userAgent); CloseableHttpClient httpClient = getClosableHttpClient(); HttpResponse httpResponse = httpClient.execute(get); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { if (httpResponse.getStatusLine().getStatusCode() == 301) { for (Header header: httpResponse.getAllHeaders()) { if (header.getName().equalsIgnoreCase("location")) { EntityUtils.consumeQuietly(httpEntity); return header.getValue(); } } EntityUtils.consumeQuietly(httpEntity); throw new IOException("redirect for " + urlstring+ ": no location attribute found"); } else { EntityUtils.consumeQuietly(httpEntity); throw new IOException("no redirect for " + urlstring+ " fail: " + httpResponse.getStatusLine().getStatusCode() + ": " + httpResponse.getStatusLine().getReasonPhrase()); } } else { throw new IOException("client connection to " + urlstring + " fail: no connection"); } }
@Override public boolean execute(final EnvironmentContext ctx, final InfrastructureComponent component) { val defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(2000) .setSocketTimeout(2000) .setConnectionRequestTimeout(2000) .build(); val httpClient = HttpClients.custom() .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(defaultRequestConfig) .build(); await().atMost(atMost.toMillis(), TimeUnit.MILLISECONDS).until(() -> { try { val response = httpClient.execute(new HttpGet(url)); val result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); return containsText != null && result.contains(containsText); } catch(final Exception ignored) { return false; } }); return true; }
/** * Simple Http Get. * * @param path the path * @return the CloseableHttpResponse * @throws URISyntaxException the URI syntax exception * @throws IOException Signals that an I/O exception has occurred. * @throws MininetException the MininetException */ public CloseableHttpResponse simpleGet(String path) throws URISyntaxException, IOException, MininetException { URI uri = new URIBuilder() .setScheme("http") .setHost(mininetServerIP.toString()) .setPort(mininetServerPort.getPort()) .setPath(path) .build(); CloseableHttpClient client = HttpClientBuilder.create().build(); RequestConfig config = RequestConfig .custom() .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS) .setSocketTimeout(CONNECTION_TIMEOUT_MS) .build(); HttpGet request = new HttpGet(uri); request.setConfig(config); request.addHeader("Content-Type", "application/json"); CloseableHttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() >= 300) { throw new MininetException(String.format("failure - received a %d for %s.", response.getStatusLine().getStatusCode(), request.getURI().toString())); } return response; }
@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); }
@Override public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException { HttpAsyncClient asyncClient = getHttpAsyncClient(); startAsyncClient(); HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri); postProcessHttpRequest(httpRequest); HttpContext context = createHttpContext(httpMethod, uri); if (context == null) { context = HttpClientContext.create(); } // Request configuration not set in the context if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) { // Use request configuration given by the user, when available RequestConfig config = null; if (httpRequest instanceof Configurable) { config = ((Configurable) httpRequest).getConfig(); } if (config == null) { config = RequestConfig.DEFAULT; } context.setAttribute(HttpClientContext.REQUEST_CONFIG, config); } return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context); }
@Override protected CloseableHttpResponse send(CloseableHttpClient httpClient, String base) throws Exception { List<NameValuePair> formParams = new ArrayList<>(); for (String key : params.keySet()) { String value = params.get(key); formParams.add(new BasicNameValuePair(key, value)); } HttpPost request = new HttpPost(base); RequestConfig localConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.STANDARD) .build(); request.setConfig(localConfig); request.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8")); request.setHeader("Content-Type", "application/x-www-form-urlencoded"); //内容为post return httpClient.execute(request); }
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(); } }
public void checkProxys(){ while(true){ Proxy proxy = this.poplProxy(); HttpHost host = new HttpHost(proxy.getIp(),proxy.getPort()); RequestConfig config = RequestConfig.custom().setProxy(host).build(); HttpGet httpGet = new HttpGet(PROXY_TEST_URL); httpGet.setConfig(config); try { CloseableHttpResponse response = this.httpClient.execute(httpGet); String content = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8")); if(content!=null&&content.trim().equals(proxy.getIp())) this.pushrProxy(proxy); } catch (IOException e) { } } }
private CloseableHttpClient getHttpClient() { int timeout = 10000; RequestConfig config = RequestConfig.custom() .setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout) .build(); return HttpClientBuilder .create() .useSystemProperties() .addInterceptorFirst(new OutboundRequestIdSettingInterceptor()) .addInterceptorFirst((HttpRequestInterceptor) new OutboundRequestLoggingInterceptor()) .addInterceptorLast((HttpResponseInterceptor) new OutboundRequestLoggingInterceptor()) .setDefaultRequestConfig(config) .build(); }
/** * HttpClient get方法请求返回Entity */ private static byte[] getMethodGetContent(String address, RequestConfig config) throws Exception { HttpGet get = new HttpGet(address); try { get.setConfig(config); HttpResponse response = CLIENT.execute(get); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { int code = response.getStatusLine().getStatusCode(); throw new RuntimeException("HttpGet Access Fail , Return Code(" + code + ")"); } response.getEntity().getContent(); return convertEntityToBytes(response.getEntity()); } finally { if (get != null) { get.releaseConnection(); } } }
private HttpUriRequest constructRequest(byte[] pduBytes, boolean useProxy) throws IOException { try { HttpPostHC4 request = new HttpPostHC4(apn.getMmsc()); for (Header header : getBaseHeaders()) { request.addHeader(header); } request.setEntity(new ByteArrayEntityHC4(pduBytes)); if (useProxy) { HttpHost proxy = new HttpHost(apn.getProxy(), apn.getPort()); request.setConfig(RequestConfig.custom().setProxy(proxy).build()); } return request; } catch (IllegalArgumentException iae) { throw new IOException(iae); } }
private CloseableHttpAsyncClient createHttpClient() { //default timeouts are all infinite RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS) .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS) .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS); if (requestConfigCallback != null) { requestConfigBuilder = requestConfigCallback.customizeRequestConfig(requestConfigBuilder); } HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build()) //default settings for connection pooling may be too constraining .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE).setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL); if (httpClientConfigCallback != null) { httpClientBuilder = httpClientConfigCallback.customizeHttpClient(httpClientBuilder); } return httpClientBuilder.build(); }
@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(); }
@Override public synchronized void start() { httpclient = HttpClientBuilder.create() .setSSLHostnameVerifier(new NoopHostnameVerifier()) .setConnectionTimeToLive(70, TimeUnit.SECONDS) .setMaxConnTotal(100) .build(); requestConfig = options.getRequestConfig(); exponentialBackOff = options.getExponentialBackOff(); if (exponentialBackOff == null) { exponentialBackOff = new ExponentialBackOff(); } if (requestConfig == null) { requestConfig = RequestConfig.copy(RequestConfig.custom().build()) .setSocketTimeout(SOCKET_TIMEOUT) .setConnectTimeout(SOCKET_TIMEOUT) .setConnectionRequestTimeout(SOCKET_TIMEOUT).build(); } super.start(); }
public HttpUriRequest toHttpUriRequest() { LOGGER.debug("in createRequestBuilder"); RequestBuilder requestBuilder = createRequestBuilder(); int timeout = httpServiceInfo.getTimeout(); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() .setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout) .setConnectTimeout(timeout) .setCookieSpec(CookieSpecs.IGNORE_COOKIES); requestBuilder.setConfig(requestConfigBuilder.build()); if (StringUtils.isNoneEmpty(httpServiceInfo.getContentType())) { requestBuilder.addHeader("Content-Type", httpServiceInfo.getContentType()); } else { requestBuilder.addHeader("Content-Type", httpServletRequest.getContentType()); } return requestBuilder.build(); }
private List<ServerAddressGroup> getServerGroups() { RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Constants.DEFAULT_TIMEOUT) .setConnectionRequestTimeout(Constants.DEFAULT_TIMEOUT).setSocketTimeout(Constants.DEFAULT_TIMEOUT) .build(); HttpClient httpClient = this.httpPool.getResource(); try { StringBuilder sb = new StringBuilder(50); sb.append(this.getServerDesc().getRegistry()) .append(this.getServerDesc().getRegistry().endsWith("/") ? "" : "/") .append(this.getServerDesc().getServerApp()).append("/servers"); HttpGet get = new HttpGet(sb.toString()); get.setConfig(requestConfig); // 创建参数队列 HttpResponse response = httpClient.execute(get); HttpEntity entity = response.getEntity(); String body = EntityUtils.toString(entity, "UTF-8"); ObjectMapper mapper = JsonMapperUtil.getJsonMapper(); return mapper.readValue(body, mapper.getTypeFactory().constructParametricType(List.class, mapper.getTypeFactory().constructType(ServerAddressGroup.class))); } catch (Exception e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } finally { this.httpPool.release(httpClient); } }
protected CloseableHttpClient constructHttpClient() throws IOException { RequestConfig config = RequestConfig.custom() .setConnectTimeout(20 * 1000) .setConnectionRequestTimeout(20 * 1000) .setSocketTimeout(20 * 1000) .setMaxRedirects(20) .build(); URL mmsc = new URL(apn.getMmsc()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); if (apn.hasAuthentication()) { credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword())); } return HttpClients.custom() .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()) .setRedirectStrategy(new LaxRedirectStrategy()) .setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT)) .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(config) .setDefaultCredentialsProvider(credsProvider) .build(); }
private synchronized void initImpl() { if (impl == null) { HttpClientBuilder builder = createBuilder(); if (receiveTimeout != null) { RequestConfig.Builder requestBuilder = RequestConfig.custom(); requestBuilder = requestBuilder.setSocketTimeout(receiveTimeout); builder.setDefaultRequestConfig(requestBuilder.build()); } for (HttpClientAdapterListener listener : listeners) { listener.onBuild(builder); } impl = builder.build(); } }
protected CloseableHttpClient createDefaultHttpClient() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(maxConnections); cm.setDefaultMaxPerRoute(maxConnections); Builder config = RequestConfig.custom() .setConnectionRequestTimeout(connectionRequestTimeout) .setConnectTimeout(connectTimeout) .setSocketTimeout(socketTimeout); // TODO: Set Credentials CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(cm).setDefaultRequestConfig(config.build()) .build(); return httpClient; }
private String executeRequest(String url, String requestStr) throws WxErrorException { HttpPost httpPost = new HttpPost(url); if (this.wxMpService.getHttpProxy() != null) { httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build()); } try (CloseableHttpClient httpclient = HttpClients.custom().build()) { httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1"))); try (CloseableHttpResponse response = httpclient.execute(httpPost)) { String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8); this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result); return result; } } catch (IOException e) { this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage()); throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e); } finally { httpPost.releaseConnection(); } }
@Override public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, IOException { HttpPost httpPost = new HttpPost(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpPost.setConfig(config); } Map<String, String> params = new HashMap<>(); params.put("media_id", materialId); httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); try(CloseableHttpResponse response = httpclient.execute(httpPost)){ String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } else { return true; } }finally { httpPost.releaseConnection(); } }
@Override public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException { if (ticket != null) { if (uri.indexOf('?') == -1) { uri += '?'; } uri += uri.endsWith("?") ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8"); } HttpGet httpGet = new HttpGet(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config); } try (CloseableHttpResponse response = httpclient.execute(httpGet); InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);) { Header[] contentTypeHeader = response.getHeaders("Content-Type"); if (contentTypeHeader != null && contentTypeHeader.length > 0) { // 出错 if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); throw new WxErrorException(WxError.fromJson(responseContent)); } } return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg"); } finally { httpGet.releaseConnection(); } }
public static int getHttpResponseStatus(String url) throws UnsupportedOperationException, IOException { // logger.trace("getHttpReponseStatus: {}", url); RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(HTTP_TIMEOUT).setConnectTimeout(HTTP_TIMEOUT) .setConnectionRequestTimeout(HTTP_TIMEOUT).build(); try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build()) { HttpGet get = new HttpGet(url); Charset chars = Charset.forName(DEFAULT_ENCODING); try (CloseableHttpResponse response = httpClient.execute(get); StringWriter writer = new StringWriter()) { return response.getStatusLine().getStatusCode(); } } }
public static void setHttpParams(HttpRequestBase httpBase, int connectMillisTimeout, int readMillisTimeout, boolean handleRedirects) { RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig) .setConnectTimeout(connectMillisTimeout) .setSocketTimeout(readMillisTimeout) .setRedirectsEnabled(handleRedirects).build(); httpBase.setConfig(requestConfig); httpBase.setHeader("accept-encoding", "gzip"); }
public HttpClient getHttpClient() { if (httpClient == null) { synchronized (this) { if (httpClient == null) { if (pool == null) { //初始化pool try { afterPropertiesSet(); } catch (Exception e) { logger.error(e.getMessage(), e); } } HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setConnectionManager(pool); httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(conntimeout).setSocketTimeout(sotimeout).build()); httpClientBuilder.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } // 否则保持活动5秒 return 5 * 1000; } }); httpClient = httpClientBuilder.build(); } } } return httpClient; }
/** * 初始化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; }
public static CloseableHttpAsyncClient create(RequestConfig requestConfig) { HttpAsyncClientBuilder builder = HttpAsyncClients.custom(); builder.setConnectionManager(connManager) .setDefaultCookieStore(cookieStore) .setDefaultCredentialsProvider(credentialsProvider); if (null != requestConfig) { return builder.setDefaultRequestConfig(requestConfig).build(); } else { return builder.setDefaultRequestConfig(defaultRequestConfig) .build(); } }
@Test public void testRequestConfigDisabledRedirects() throws URISyntaxException, IOException { { HttpClient client = clientBuilder .setDefaultRequestConfig(RequestConfig.custom() .setRedirectsEnabled(false) .build()) .build(); client.execute(new HttpGet(serverUrl(RedirectHandler.MAPPING))); } List<MockSpan> mockSpans = mockTracer.finishedSpans(); Assert.assertEquals(2, mockSpans.size()); MockSpan mockSpan = mockSpans.get(0); Assert.assertEquals("GET", mockSpan.operationName()); Assert.assertEquals(6, mockSpan.tags().size()); Assert.assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey())); Assert.assertEquals("GET", mockSpan.tags().get(Tags.HTTP_METHOD.getKey())); Assert.assertEquals(serverUrl("/redirect"), mockSpan.tags().get(Tags.HTTP_URL.getKey())); Assert.assertEquals(301, mockSpan.tags().get(Tags.HTTP_STATUS.getKey())); Assert.assertEquals(serverHost.getPort(), mockSpan.tags().get(Tags.PEER_PORT.getKey())); Assert.assertEquals(serverHost.getHostName(), mockSpan.tags().get(Tags.PEER_HOSTNAME.getKey())); Assert.assertEquals(0, mockSpan.logEntries().size()); assertLocalSpan(mockSpans.get(1)); }
/** * 对上一个方法的重载,使用本机ip进行网站爬取 */ public static String getHtml1(String url) throws ClassNotFoundException, IOException { String entity = null; CloseableHttpClient httpClient = HttpClients.createDefault(); //设置超时处理 RequestConfig config = RequestConfig.custom().setConnectTimeout(5000). setSocketTimeout(5000).build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(config); httpGet.setHeader("Accept", "*/*"); httpGet.setHeader("Accept-Encoding", "gzip, deflate, sdch"); httpGet.setHeader("Content-Type", " text/plain;charset=utf-8"); httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8"); httpGet.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"); try { //客户端执行httpGet方法,返回响应 CloseableHttpResponse httpResponse = httpClient.execute(httpGet); //得到服务响应状态码 if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { entity = EntityUtils.toString(httpResponse.getEntity(), "utf-8"); } httpResponse.close(); httpClient.close(); } catch (ClientProtocolException e) { e.printStackTrace(); } return entity; }
private HttpUriRequest constructRequest(Apn contentApn, boolean useProxy) throws IOException { HttpGetHC4 request = new HttpGetHC4(contentApn.getMmsc()); for (Header header : getBaseHeaders()) { request.addHeader(header); } if (useProxy) { HttpHost proxy = new HttpHost(contentApn.getProxy(), contentApn.getPort()); request.setConfig(RequestConfig.custom().setProxy(proxy).build()); } return request; }
private HttpClient createHttpClient(Authentication auth, String verify, HttpHost target, Boolean postRedirects, String password, TrustStrategy keystoreTrustStrategy, HostnameVerifier keystoreHostnameVerifier, Proxy proxy) { Certificate certificate = new Certificate(); Auth authHelper = new Auth(); HttpClientBuilder httpClientBuilder = WinHttpClients.custom(); Builder requestConfig = RequestConfig.custom(); requestConfig.setCookieSpec(CookieSpecs.DEFAULT); logger.debug("Verify value: " + verify); logger.debug((new File(verify).getAbsolutePath())); if (new File(verify).exists()) { logger.debug("Loading custom keystore"); httpClientBuilder.setSSLSocketFactory( certificate.allowAllCertificates(certificate.createCustomKeyStore(verify.toString(), password), password, keystoreTrustStrategy, keystoreHostnameVerifier)); } else if (!Boolean.parseBoolean(verify.toString())) { logger.debug("Allowing all certificates"); httpClientBuilder.setSSLSocketFactory(certificate.allowAllCertificates(null)); } if (auth.isAuthenticable()) { httpClientBuilder.setDefaultCredentialsProvider(authHelper.getCredentialsProvider(auth, target)); } if (proxy != null && proxy.isInUse()) { logger.debug("Enabling proxy"); if (proxy.isAuthenticable()) { logger.debug("Setting proxy credentials"); httpClientBuilder.setDefaultCredentialsProvider( authHelper.getCredentialsProvider(proxy.getAuth(), proxy.getHttpHost())); } requestConfig.setProxy(proxy.getHttpHost()); } if (postRedirects) { httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy()); } httpClientBuilder.setDefaultRequestConfig(requestConfig.build()); return httpClientBuilder.build(); }
/** * 获取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(); }
private CloseableHttpClient createHttpClient() { final RequestConfig requestConfig = RequestConfig.custom() .setCookieSpec(CookieSpecs.DEFAULT) .setAuthenticationEnabled(true) .setRedirectsEnabled(true) .build(); return HttpClientBuilder.create() .setDefaultCookieStore(cookieStore) .setDefaultRequestConfig(requestConfig) .build(); }
private static void init() { context = HttpClientContext.create(); cookieStore = new BasicCookieStore(); // 配置超时时间(连接服务端超时1秒,请求数据返回超时2秒) RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(120000).setSocketTimeout(60000) .setConnectionRequestTimeout(60000).build(); // 设置默认跳转以及存储cookie httpClient = HttpClientBuilder.create() .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) .setRedirectStrategy(new DefaultRedirectStrategy()).setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore).build(); }
@Test public void testPost() throws IOException { String ip = "冰箱冰箱冰箱冰箱冰箱冰箱冰箱"; // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); // 请求参数 StringEntity entity = new StringEntity("", DEFAULT_ENCODE); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON)); HttpPost httpPost = new HttpPost("https://m.fangliaoyun.com"); httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON); //此处区别PC终端类型 httpPost.addHeader("typeFlg", "9"); //此处增加浏览器端访问IP httpPost.addHeader("x-forwarded-for", ip); httpPost.addHeader("Proxy-Client-IP", ip); httpPost.addHeader("WL-Proxy-Client-IP", ip); httpPost.addHeader("HTTP_CLIENT_IP", ip); httpPost.addHeader("X-Real-IP", ip); httpPost.addHeader("Host", ip); httpPost.setEntity(entity); httpPost.setConfig(RequestConfig.DEFAULT); HttpResponse httpResponse; // post请求 httpResponse = closeableHttpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); System.out.println(httpEntity.getContent()); //释放资源 closeableHttpClient.close(); }
/** * Post Entity */ private static byte[] getMethodPostContent(String address, HttpEntity paramEntity, RequestConfig config) throws Exception { HttpPost post = new HttpPost(address); try { if (paramEntity != null) { post.setEntity(paramEntity); } post.setConfig(config); HttpResponse response = CLIENT.execute(post); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { int code = response.getStatusLine().getStatusCode(); throw new RuntimeException( "HttpPost Request Access Fail Return Code(" + code + ")"); } HttpEntity entity = response.getEntity(); if (entity == null) { throw new RuntimeException( "HttpPost Request Access Fail response Entity Is null"); } return convertEntityToBytes(entity); } finally { if (post != null) { post.releaseConnection(); } } }
public int sendMessage(StatMessage msg, long retryTimes) { String content = null; HttpResponse response = null; try { post.setURI(uri); // add header content = statMessageToLineProtocol(msg); post.setEntity(new StringEntity(content)); post.setConfig(RequestConfig.custom().setConnectionRequestTimeout(CUSTOM_TIME_OUT).setConnectTimeout(CUSTOM_TIME_OUT).setSocketTimeout(CUSTOM_TIME_OUT).build()); response = client.execute(post); int code = response.getStatusLine().getStatusCode(); if (code == 200 || code == 204) { LOG.info(String.format("Sink to influxdb OK! http_code=%d, content=%s", code, content)); return 0; } else { LOG.warn(String.format("http_code=%d! try %d times -- Sink to influxdb failed! url=%s, content=%s", code, retryTimes, postURL, content)); initPost(); return -1; } } catch (Exception e) { LOG.warn(String.format("Reason:%s. try %d times -- Sink to influxdb failed! url=%s, content=%s", e.getMessage(), retryTimes, postURL, content)); initPost(); return -1; } }
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(); } }