List of usage examples for org.apache.http.client HttpClient getConnectionManager
@Deprecated ClientConnectionManager getConnectionManager();
From source file:net.datacrow.onlinesearch.discogs.task.DiscogsMusicSearch.java
@Override protected Collection<Object> getItemKeys() throws Exception { HttpClient httpClient = getHttpClient(); List<NameValuePair> queryParams = new ArrayList<NameValuePair>(); queryParams.add(new BasicNameValuePair("q", getQuery())); URIBuilder builder = new URIBuilder(); builder.setScheme("http"); builder.setHost("api.discogs.com"); builder.setPath("/database/search"); builder.setQuery(URLEncodedUtils.format(queryParams, "UTF-8")); URI uri = builder.build();/*from w ww .j ava 2s . c om*/ HttpGet httpGet = new HttpGet(uri); HttpOAuthHelper au = new HttpOAuthHelper("application/json"); au.handleRequest(httpGet, _CONSUMER_KEY, _CONSUMER_SECRET, null, queryParams); HttpResponse httpResponse = httpClient.execute(httpGet); String response = getReponseText(httpResponse); httpClient.getConnectionManager().shutdown(); Collection<Object> keys = new ArrayList<Object>(); int counter = 0; for (String key : StringUtils.getValuesBetween("\"id\":", "}", response)) { keys.add(key.trim()); if (counter++ >= maxQuerySize) break; } return keys; }
From source file:org.anon.smart.smcore.test.channel.upload.TestUploadEvent.java
private void uploadFile(String file) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost:9020" + "/errortenant/ErrorCases/UploadEvent"); FileBody bin = new FileBody(new File(file)); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("bin", bin); httppost.setEntity(reqEntity);/*from w w w . ja v a 2 s . c o m*/ System.out.println("executing request " + httppost.getRequestLine()); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String response = null; try { response = httpclient.execute(httppost, responseHandler); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } httpclient.getConnectionManager().shutdown(); }
From source file:ch.entwine.weblounge.test.harness.content.HTMLActionTest.java
/** * Tests whether actions are rendered using templates as configured in * module.xml// w w w.ja va2 s . c om * * @param serverUrl * the server url */ private void testConfiguredTemplate(String serverUrl) { logger.info("Preparing test of greeter action with configured template"); HttpGet request = new HttpGet(UrlUtils.concat(serverUrl, templatedActionPath)); // Send the request and make sure it ends up on the expected page logger.info("Sending request to {}", request.getURI()); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = TestUtils.request(httpClient, request, null); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); // Get the document contents Document xml = TestUtils.parseXMLResponse(response); // Make sure it is rendered on the home page String templateTitle = XPathHelper.valueOf(xml, "/html/head/title"); assertEquals("Action is not rendered on alternate template", ALTERNATE_TEMPLATE_TITLE, templateTitle); } catch (Throwable e) { fail("Request to " + request.getURI() + " failed" + e.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:com.whp.android.rest.RestClient.java
/** * getInputStream/*from w w w.j av a 2 s .c o m*/ * @return */ public InputStream getInputStream() { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParameters, CONNECTION_TIMEOUT); HttpClient client = new DefaultHttpClient(httpParameters); HttpResponse httpResponse; try { HttpPost request = new HttpPost(url); httpResponse = client.execute(request); responseCode = httpResponse.getStatusLine().getStatusCode(); message = httpResponse.getStatusLine().getReasonPhrase(); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); return instream; /* * response = convertStreamToString(instream); * * // Closing the input stream will trigger connection release * instream.close(); */ } } catch (ClientProtocolException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } catch (IOException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } return null; }
From source file:ch.entwine.weblounge.test.harness.content.HTMLActionTest.java
/** * Tests whether action template can be overwritten. * //from w w w . j a v a2 s . com * @param serverUrl * the server url */ private void testOverridenTemplateByParameter(String serverUrl) { logger.info("Preparing test of greeter action with overridden template by parameter"); StringBuffer requestUrl = new StringBuffer(defaultActionPath); requestUrl.append("?").append(HTMLAction.TARGET_TEMPLATE).append("=alternate"); HttpGet request = new HttpGet(UrlUtils.concat(serverUrl, requestUrl.toString())); // Send the request and make sure it ends up on the expected page logger.info("Sending request to {}", request.getURI()); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = TestUtils.request(httpClient, request, null); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); // Get the document contents Document xml = TestUtils.parseXMLResponse(response); // Make sure it is rendered on the home page String templateTitle = XPathHelper.valueOf(xml, "/html/head/title"); assertEquals("Action is not rendered on alternate template", ALTERNATE_TEMPLATE_TITLE, templateTitle); } catch (Throwable e) { fail("Request to " + request.getURI() + " failed" + e.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:ch.entwine.weblounge.test.harness.content.HTMLActionTest.java
/** * Tests whether action template can be overwritten. The template is * overwritten in the configure() method rather than by a request parameter. * /*from www . j ava2 s.c om*/ * @param serverUrl * the server url */ private void testOverridenTemplateByCode(String serverUrl) { logger.info("Preparing test of greeter action with overridden template by code"); StringBuffer requestUrl = new StringBuffer(defaultActionPath); requestUrl.append("?").append(GreeterHTMLAction.CODE_TEMPLATE).append("=alternate"); HttpGet request = new HttpGet(UrlUtils.concat(serverUrl, requestUrl.toString())); // Send the request and make sure it ends up on the expected page logger.info("Sending request to {}", request.getURI()); HttpClient httpClient = new DefaultHttpClient(); try { HttpResponse response = TestUtils.request(httpClient, request, null); assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode()); // Get the document contents Document xml = TestUtils.parseXMLResponse(response); // Make sure it is rendered on the home page String templateTitle = XPathHelper.valueOf(xml, "/html/head/title"); assertEquals("Action is not rendered on alternate template", ALTERNATE_TEMPLATE_TITLE, templateTitle); } catch (Throwable e) { fail("Request to " + request.getURI() + " failed" + e.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:com.meschbach.psi.util.RESTClient.java
/** * Issues the request represented by this client and notifies the response * handlers of th results of the request. * * @throws PSIException if a problem occurs issuing or processing the request * @throws AssertionError maybe thrown depending on the implementation of the response handlers *///w w w. java 2s .com public void doRequest() throws PSIException { /* * Issue the request */ HttpClient client = new DefaultHttpClient(); try { /* * Escape our URL */ URI uri = new URI(url); /* * Construct and issue request */ HttpResponse response = client.execute(builder.buildRequest(uri)); /* * Ensure the response code is as expected */ HttpStatusCode responseCode = HttpStatusCode.getCode(response.getStatusLine().getStatusCode()); Iterator<ResponseHandler> rhit = handlers.iterator(); while (rhit.hasNext()) { rhit.next().handleResponse(response, responseCode); } } catch (URISyntaxException use) { throw new PSIException(use); } catch (IOException ioe) { throw new PSIException(ioe); } finally { client.getConnectionManager().shutdown(); } }
From source file:org.sufficientlysecure.keychain.keyimport.HkpKeyServer.java
@Override public void add(String armoredKey) throws AddKeyException { HttpClient client = new DefaultHttpClient(); try {//from w ww . j a v a 2 s. c o m String query = "http://" + mHost + ":" + mPort + "/pks/add"; HttpPost post = new HttpPost(query); Log.d(Constants.TAG, "hkp keyserver add: " + query); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("keytext", armoredKey)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new AddKeyException(); } } catch (IOException e) { // nothing to do, better luck on the next keyserver } finally { client.getConnectionManager().shutdown(); } }