public HttpClient(int maxConPerHost, int conTimeOutMs, int soTimeOutMs, int maxSize) { // MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(true); HttpConnectionManagerParams params = connectionManager.getParams(); params.setDefaultMaxConnectionsPerHost(maxConPerHost); params.setConnectionTimeout(conTimeOutMs); params.setSoTimeout(soTimeOutMs); HttpClientParams clientParams = new HttpClientParams(); clientParams.setCookiePolicy(CookiePolicy.IGNORE_COOKIES); client = new org.apache.commons.httpclient.HttpClient(clientParams, connectionManager); Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443); Protocol.registerProtocol("https", myhttps); }
private void shutdown(HttpMethodBase method) { if (method != null) { method.releaseConnection(); } if (!externalConnectionManager) { ((SimpleHttpConnectionManager) httpConnectionManager).shutdown(); } }
private void initHttpClient() { HostConfiguration hostConfiguration = new HostConfiguration(); SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); connectionManager.closeIdleConnections(5000L); HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled()); params.setConnectionTimeout(diamondConfigure.getConnectionTimeout()); connectionManager.setParams(params); configHttpClient = new HttpClient(connectionManager); configHttpClient.setHostConfiguration(hostConfiguration); }
/** {@inheritDoc} */ @Override public boolean interrupt() { HttpClient client = savedClient; if (client != null) { savedClient = null; // TODO - not sure this is the best method final HttpConnectionManager httpConnectionManager = client.getHttpConnectionManager(); if (httpConnectionManager instanceof SimpleHttpConnectionManager) {// Should be true ((SimpleHttpConnectionManager)httpConnectionManager).shutdown(); } } return client != null; }
/** * {@inheritDoc} */ @Override public void start() throws HomematicClientException { logger.info("Starting {}", CcuClient.class.getSimpleName()); super.start(); tclregaScripts = loadTclRegaScripts(); httpClient = new HttpClient(new SimpleHttpConnectionManager(true)); HttpClientParams params = httpClient.getParams(); Long timeout = context.getConfig().getTimeout() * 1000L; params.setConnectionManagerTimeout(timeout); params.setSoTimeout(timeout.intValue()); params.setContentCharset("ISO-8859-1"); }
private void initHttpClient() { HostConfiguration hostConfiguration = new HostConfiguration(); SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); connectionManager.closeIdleConnections(5000L); HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setStaleCheckingEnabled(diamondConfigure .isConnectionStaleCheckingEnabled()); params.setConnectionTimeout(diamondConfigure.getConnectionTimeout()); connectionManager.setParams(params); configHttpClient = new HttpClient(connectionManager); configHttpClient.setHostConfiguration(hostConfiguration); }
@Test(expected=IOException.class) public void testFailConnection() throws Exception { SimpleHttpConnectionManager simple = new SimpleHttpConnectionManager(); HttpClient http = new HttpClient(simple); ReviewboardConnection bad = new ReviewboardConnection(System.getProperty("reviewboard.url", "https://reviewboard.eng.vmware.com/"), System.getProperty("reviewboard.user"), "foobar"); try { ReviewboardOps.ensureAuthentication(bad, http); } finally { simple.shutdown(); } }
private static boolean isGovCloud() /* */ { /* 486 */ if (ec2MetaDataAz != null) { /* 487 */ return ec2MetaDataAz.startsWith("us-gov-west-1"); /* */ } /* */ /* 492 */ String hostname = getHostName(); /* 493 */ int timeout = hostname.startsWith("ip-") ? 30000 : 5000; /* 494 */ GetMethod getMethod = new GetMethod("http://169.254.169.254/latest/meta-data/placement/availability-zone"); /* */ try { /* 496 */ HttpConnectionManager manager = new SimpleHttpConnectionManager(); /* 497 */ HttpConnectionManagerParams params = manager.getParams(); /* */ /* 499 */ params.setConnectionTimeout(timeout); /* */ /* 501 */ params.setSoTimeout(timeout); /* 502 */ HttpClient httpClient = new HttpClient(manager); /* 503 */ int status = httpClient.executeMethod(getMethod); /* 504 */ if ((status < 200) || (status > 299)) { /* 505 */ LOG.info("error status code" + status + " GET " + "http://169.254.169.254/latest/meta-data/placement/availability-zone"); /* */ } else { /* 507 */ ec2MetaDataAz = getMethod.getResponseBodyAsString().trim(); /* 508 */ LOG.info("GET http://169.254.169.254/latest/meta-data/placement/availability-zone result: " + ec2MetaDataAz); /* 509 */ return ec2MetaDataAz.startsWith("us-gov-west-1"); /* */ } /* */ } catch (Exception e) { /* 512 */ LOG.info("GET http://169.254.169.254/latest/meta-data/placement/availability-zone exception ", e); /* */ } finally { /* 514 */ getMethod.releaseConnection(); /* */ } /* 516 */ return false; /* */ }
private void initDownloadThread() { client.getHTTPClient().setHttpConnectionManager(new SimpleHttpConnectionManager()); client.getHTTPClient().getHttpConnectionManager().closeIdleConnections(0); }
/** * 通过http的post方式请求数据 connectionTimeOut:建立连接的超时时间,soTimeOut:等待返回结果的超时时间 * @param urlString * @param params * @param encode * @param connectionTimeOut * @param soTimeOut * @return * @throws IOException * @throws HttpException * @throws Exception * @Author YHJ create at 2014年5月14日 下午4:36:02 */ public static String doPost(String urlString, Map<String, String> params) throws HttpException, IOException { PostMethod method = new PostMethod(urlString); HttpClient client = null; try { Set<String> keys = params.keySet(); NameValuePair[] values = new NameValuePair[keys.size()]; int i = 0; for (String key : keys) { NameValuePair v = new NameValuePair(); v.setName(key); v.setValue(params.get(key)); values[i] = v; i++; } client = new HttpClient(); client.getHostConfiguration().setHost(urlString, 80, "http"); client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);// 建立连接的超时时间 client.getHttpConnectionManager().getParams().setSoTimeout(30000);// 等待请求结果的超时时间 if (StringUtils.isNotBlank(encode)) client.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, encode); method.setRequestBody(values); // 使用 POST 方式提交数据 int state = client.executeMethod(method); //返回的状态 if(state != HttpStatus.SC_OK){ throw new RuntimeException("HttpStatus is "+state); } return inputStreamToString(method.getResponseBodyAsStream(), encode); } finally { //releaseConnection方法不能关闭socket连接 //使用SimpleHttpConnectionManager的shutdown方法强制关闭 method.releaseConnection(); if (client != null ) { HttpConnectionManager manager = client.getHttpConnectionManager(); if (manager instanceof SimpleHttpConnectionManager) { SimpleHttpConnectionManager tmp = (SimpleHttpConnectionManager)manager; tmp.shutdown(); } } } }
/** * This method performs the proxying of the request to the target address. * * @param target The target address. Has to be a fully qualified address. The request is send as-is to this address. * @param hsRequest The request data which should be send to the * @param hsResponse The response data which will contain the data returned by the proxied request to target. * @throws java.io.IOException Passed on from the connection logic. */ public static void execute(final String target, final HttpServletRequest hsRequest, final HttpServletResponse hsResponse) throws IOException { if ( log.isInfoEnabled() ) { log.info("execute, target is " + target); log.info("response commit state: " + hsResponse.isCommitted()); } if (StringUtils.isBlank(target)) { log.error("The target address is not given. Please provide a target address."); return; } log.info("checking url"); final URL url; try { url = new URL(target); } catch (MalformedURLException e) { log.error("The provided target url is not valid.", e); return; } log.info("seting up the host configuration"); final HostConfiguration config = new HostConfiguration(); ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy")); if (proxyHost != null) config.setProxyHost(proxyHost); final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort(); config.setHost(url.getHost(), port, url.getProtocol()); if ( log.isInfoEnabled() ) log.info("config is " + config.toString()); final HttpMethod targetRequest = setupProxyRequest(hsRequest, url); if (targetRequest == null) { log.error("Unsupported request method found: " + hsRequest.getMethod()); return; } //perform the reqeust to the target server final HttpClient client = new HttpClient(new SimpleHttpConnectionManager()); if (log.isInfoEnabled()) { log.info("client state" + client.getState()); log.info("client params" + client.getParams().toString()); log.info("executeMethod / fetching data ..."); } final int result; if (targetRequest instanceof EntityEnclosingMethod) { final RequestProxyCustomRequestEntity requestEntity = new RequestProxyCustomRequestEntity( hsRequest.getInputStream(), hsRequest.getContentLength(), hsRequest.getContentType()); final EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) targetRequest; entityEnclosingMethod.setRequestEntity(requestEntity); result = client.executeMethod(config, entityEnclosingMethod); } else { result = client.executeMethod(config, targetRequest); } //copy the target response headers to our response setupResponseHeaders(targetRequest, hsResponse); InputStream originalResponseStream = targetRequest.getResponseBodyAsStream(); //the body might be null, i.e. for responses with cache-headers which leave out the body if (originalResponseStream != null) { OutputStream responseStream = hsResponse.getOutputStream(); copyStream(originalResponseStream, responseStream); } log.info("set up response, result code was " + result); }