List of usage examples for org.apache.http.client HttpClient getConnectionManager
@Deprecated ClientConnectionManager getConnectionManager();
From source file:net.sourceforge.subsonic.controller.ProxyController.java
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String url = ServletRequestUtils.getRequiredStringParameter(request, "url"); HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); HttpConnectionParams.setSoTimeout(client.getParams(), 15000); HttpGet method = new HttpGet(url); InputStream in = null;//from www . ja v a 2 s .com try { HttpResponse resp = client.execute(method); int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { response.sendError(statusCode); } else { in = resp.getEntity().getContent(); IOUtils.copy(in, response.getOutputStream()); } } finally { IOUtils.closeQuietly(in); client.getConnectionManager().shutdown(); } return null; }
From source file:com.github.thebigs.nzbmatrixapi.NZBMatrixApi.java
/** * Does the hevy lifting of making the http GET request to the nzbmatrix api using apache's http client libraries * * @param uri web address to access (including params) * @param parser parser to call when a response is received * * @return returns a subclass of Response, this should be casted to the type of response returned by the ResponseParser given * * @throws ClientProtocolException//w w w . j ava 2 s.c o m * @throws IOException * @throws NZBMatrixApiException */ private Response doApiCall(final String uri, final Parser parser) throws ClientProtocolException, IOException, NZBMatrixApiException { final HttpClient httpclient = new DefaultHttpClient(); final HttpGet httpget = new HttpGet(uri); // uncomment this to see all of the request queries as they are submitted. // System.out.println("executing request " + httpget.getURI()); // Create a response handler final ResponseHandler<String> responseHandler = new BasicResponseHandler(); final String responseBody = httpclient.execute(httpget, responseHandler); httpclient.getConnectionManager().shutdown(); return parser.parseResponse(responseBody); }
From source file:org.apache.camel.component.cxf.cxfbean.CxfBeanTest.java
private String getCustomerId(String name) throws Exception { HttpGet get = new HttpGet("http://localhost:" + PORT1 + "/customerservice/customers/"); get.addHeader("Accept", "application/xml"); HttpClient httpclient = new DefaultHttpClient(); try {/*from w w w. j av a 2 s . c o m*/ HttpResponse response = httpclient.execute(get); assertEquals(200, response.getStatusLine().getStatusCode()); String customers = EntityUtils.toString(response.getEntity()); String before = ObjectHelper.before(customers, "</id><name>" + name + "</name></Customer>"); String answer = before.substring(before.lastIndexOf(">") + 1, before.length()); return answer; } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:com.cloudera.flume.handlers.hive.MarkerStore.java
public boolean sendESQuery(String elasticSearchUrl, String sb) { boolean success = true; LOG.info("sending batched stringentities"); LOG.info("elasticSearchUrl: " + elasticSearchUrl); try {/* w ww .ja va2 s . c o m*/ HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(elasticSearchUrl); StringEntity se = new StringEntity(sb); httpPost.setEntity(se); HttpResponse hr = httpClient.execute(httpPost); LOG.info("HTTP Response: " + hr.getStatusLine()); LOG.info("Closing httpConnection"); httpClient.getConnectionManager().shutdown(); LOG.info("booooooo: " + CharStreams.toString(new InputStreamReader(se.getContent()))); } catch (IOException e) { e.printStackTrace(); success = false; } finally { if (!success) { LOG.info("ESQuery wasn't successful, writing to markerfolder"); writeElasticSearchToMarkerFolder(new StringBuilder(sb)); } } LOG.info("ESQuery was successful, yay!"); return success; }
From source file:de.hanbei.resources.http.HttpResourceLocator.java
public InputStream getResource(String name) { HttpClient client = new DefaultHttpClient(); try {// w w w .j a va2 s.c om URI resourceUri = new URI(name); HttpGet get = new HttpGet(resourceBase.resolve(resourceUri)); HttpResponse response = client.execute(get); if (200 == response.getStatusLine().getStatusCode()) { return response.getEntity().getContent(); } else { LOGGER.warn("Resource {} not found on base {}", name, resourceBase); return null; } } catch (URISyntaxException e) { // exceptions are allowed as null will be returned. } catch (ClientProtocolException e) { } catch (IOException e) { } finally { client.getConnectionManager().shutdown(); } LOGGER.warn("Resource {} not found on base {}", name, resourceBase); return null; }
From source file:com.lostad.app.base.util.RequestUtil.java
public static String putForm(String url, List<NameValuePair> params, String token, boolean isSingleton) throws Exception { String json = null;/* w w w . j ava2s . c o m*/ HttpClient client = HttpClientManager.getHttpClient(isSingleton); HttpEntity resEntity = null; HttpPut put = new HttpPut(url); put.addHeader("token", token); try { put.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); if (params != null) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); if (params != null) { entity.setContentEncoding(HTTP.UTF_8); entity.setContentType("application/x-www-form-urlencoded"); } put.setEntity(entity); } HttpResponse response = client.execute(put, new BasicHttpContext()); resEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 200 // ? json = EntityUtils.toString(resEntity); } } catch (Exception e) { if (put != null) { put.abort();// } throw e; } finally { if (resEntity != null) { try { resEntity.consumeContent();//? } catch (IOException e) { e.printStackTrace(); } } client.getConnectionManager().closeExpiredConnections(); ///client.getConnectionManager().shutdown(); } return json; }
From source file:uk.bcu.services.MusicSearchService.java
public void run() { String api_key = "f06e41f0aad377a1aebfe76927318181"; String url = "http://ws.audioscrobbler.com/2.0/?method=album.search&album=" + query + "&api_key=" + api_key + "&format=json"; boolean error = false; HttpClient httpclient = null; try {// w w w .jav a 2 s . c o m httpclient = new DefaultHttpClient(); HttpResponse data = httpclient.execute(new HttpGet(url)); HttpEntity entity = data.getEntity(); String result = EntityUtils.toString(entity, "UTF8"); JSONObject json = new JSONObject(result); if (Integer.valueOf(json.getJSONObject("results").getString("opensearch:totalResults")) > 0) { results = json.getJSONObject("results").getJSONObject("albummatches").getJSONArray("album"); } else { error = true; } } catch (Exception e) { results = null; error = true; } finally { httpclient.getConnectionManager().shutdown(); } super.serviceComplete(error); }
From source file:net.sourceforge.subsonic.backend.controller.IPNController.java
private boolean validate(String url) throws Exception { HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 60000); HttpConnectionParams.setSoTimeout(client.getParams(), 60000); HttpGet method = new HttpGet(url); String content;/*from w w w. ja v a 2 s . co m*/ try { ResponseHandler<String> responseHandler = new BasicResponseHandler(); content = client.execute(method, responseHandler); LOG.info("Validation result: " + content); return "VERIFIED".equals(content); } finally { client.getConnectionManager().shutdown(); } }
From source file:org.jboss.as.test.integration.web.security.cert.WebSecurityCERTTestCase.java
public static HttpClient wrapClient(HttpClient base, String alias) { try {/*www . j a v a 2s .c om*/ SSLContext ctx = SSLContext.getInstance("TLS"); JBossJSSESecurityDomain jsseSecurityDomain = new JBossJSSESecurityDomain("client-cert"); jsseSecurityDomain.setKeyStorePassword("changeit"); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); URL keystore = tccl.getResource("security/client.keystore"); jsseSecurityDomain.setKeyStoreURL(keystore.getPath()); jsseSecurityDomain.setClientAlias(alias); jsseSecurityDomain.reloadKeyAndTrustStore(); KeyManager[] keyManagers = jsseSecurityDomain.getKeyManagers(); TrustManager[] trustManagers = jsseSecurityDomain.getTrustManagers(); ctx.init(keyManagers, trustManagers, null); X509HostnameVerifier verifier = new X509HostnameVerifier() { @Override public void verify(String s, SSLSocket sslSocket) throws IOException { } @Override public void verify(String s, X509Certificate x509Certificate) throws SSLException { } @Override public void verify(String s, String[] strings, String[] strings1) throws SSLException { } @Override public boolean verify(String string, SSLSession ssls) { return true; } }; SSLSocketFactory ssf = new SSLSocketFactory(ctx, verifier);//SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = base.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 8380, ssf)); return new DefaultHttpClient(ccm, base.getParams()); } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:org.wso2.carbon.appfactory.apiManager.integration.APIManagerIntegrationService.java
public boolean generateKeys(String appId, String samlToken) throws AppFactoryException { HttpClient httpClient = new DefaultHttpClient(); try {/*from w w w. j a va 2 s . c om*/ loginToStore(httpClient, samlToken); URL apiManagerUrl = getApiManagerURL(); generateKey(appId, apiManagerUrl, "SANDBOX", httpClient); generateKey(appId, apiManagerUrl, "PRODUCTION", httpClient); return true; } finally { httpClient.getConnectionManager().shutdown(); } }