List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBody
public abstract byte[] getResponseBody() throws IOException;
From source file:com.netsteadfast.greenstep.sys.CxfServerBean.java
@SuppressWarnings("unchecked") public static Map<String, Object> shutdownOrReloadCallOneSystem(HttpServletRequest request, String system, String type) throws ServiceException, Exception { if (StringUtils.isBlank(system) || StringUtils.isBlank(type)) { throw new ServiceException(SysMessageUtil.get(GreenStepSysMsgConstants.PARAMS_BLANK)); }/* w w w . j av a2 s .com*/ String urlStr = ApplicationSiteUtils.getBasePath(system, request) + "config-services?type=" + type + "&value=" + createParamValue(); logger.info("shutdownOrReloadCallSystem , url=" + urlStr); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(urlStr); client.executeMethod(method); byte[] responseBody = method.getResponseBody(); if (null == responseBody) { throw new Exception("no response!"); } String content = new String(responseBody, Constants.BASE_ENCODING); logger.info("shutdownOrReloadCallSystem , system=" + system + " , type=" + type + " , response=" + content); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> dataMap = null; try { dataMap = (Map<String, Object>) mapper.readValue(content, HashMap.class); } catch (JsonParseException e) { logger.error(e.getMessage().toString()); } catch (JsonMappingException e) { logger.error(e.getMessage().toString()); } if (null == dataMap) { throw new Exception("response content error!"); } return dataMap; }
From source file:com.netsteadfast.greenstep.util.ApplicationSiteUtils.java
private static boolean checkTestConnection(String host, String contextPath, HttpServletRequest request) { boolean test = false; String basePath = request.getScheme() + "://" + host + "/" + contextPath; String urlStr = basePath + "/pages/system/testJsonResult.action"; try {/*from ww w .jav a 2 s .c om*/ logger.info("checkTestConnection , url=" + urlStr); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(urlStr); HttpClientParams params = new HttpClientParams(); params.setConnectionManagerTimeout(TEST_JSON_HTTP_TIMEOUT); client.setParams(params); client.executeMethod(method); byte[] responseBody = method.getResponseBody(); if (null == responseBody) { test = false; return test; } String content = new String(responseBody, Constants.BASE_ENCODING); ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") Map<String, Object> dataMap = (Map<String, Object>) mapper.readValue(content, HashMap.class); if (YesNo.YES.equals(dataMap.get("success"))) { test = true; } } catch (JsonParseException e) { logger.error(e.getMessage().toString()); } catch (JsonMappingException e) { logger.error(e.getMessage().toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (!test) { logger.warn("checkTestConnection : " + String.valueOf(test)); } else { logger.info("checkTestConnection : " + String.valueOf(test)); } } return test; }
From source file:fr.matriciel.AnnotationClient.java
public static String request(HttpMethod method) throws AnnotationException { String response = null;//www .j a v a 2 s . co m // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.out.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. // Deal with the response. // Use caution: ensure correct character encoding and is not binary data response = new String(responseBody); } catch (HttpException e) { throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:mesquite.tol.lib.BaseHttpRequestMaker.java
protected static boolean executeMethod(HttpClient client, HttpMethod method, StringBuffer response) { boolean success = true; try {//from w w w. j a va2s. c o m // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); if (response != null) response.append(new String(responseBody)); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data System.out.println(new String(responseBody)); } catch (HttpException e) { // System.err.println("Fatal protocol violation: " + e.getMessage()); // e.printStackTrace(); success = false; } catch (IOException e) { // System.err.println("Fatal transport error: " + e.getMessage()); // e.printStackTrace(); success = false; } finally { // Release the connection. method.releaseConnection(); } return success; }
From source file:com.bigrocksoftware.metarparser.MetarFetcher.java
public static String fetch(String station, int timeout) { metarData = null;/*from w w w . ja v a 2 s.c o m*/ // create the http client HttpClient client = new HttpClient(); // set the timeout is specified if (timeout != 0) { log.debug("MetarFetch: setting timeout to '" + timeout + "' milliseconds"); long start = System.currentTimeMillis(); client.setConnectionTimeout(timeout); long end = System.currentTimeMillis(); if (end - start < timeout) { client.setTimeout((int) (end - start)); } else { return null; } } // create the http method we will use HttpMethod method = new GetMethod(httpMetarURL + station + ".TXT"); // connect to the NOAA site, retrying up to the specified num int statusCode = -1; //for (int attempt = 0; statusCode == -1 && attempt < 3; attempt++) { try { // execute the get method log.debug("MetarFetcher: downloading data for station '" + station + "'"); statusCode = client.executeMethod(method); } catch (HttpRecoverableException e) { log.error("a recoverable exception occurred, " + "retrying." + e.getMessage()); } catch (IOException e) { log.error("failed to download file: " + e); } //} // check that we didn't run out of retries if (statusCode != HttpStatus.SC_OK) { log.error("failed to download station data for '" + station + "'"); return null; } else { // read the response body byte[] responseBody = method.getResponseBody(); // release the connection method.releaseConnection(); // deal with the response. // FIXME - ensure we use the correct character encoding here metarData = new String(responseBody) + "\n"; log.debug("MetarFetcher: metar data: " + metarData); } return metarData; }
From source file:de.loercher.localpress.connector.HttpClientJob.java
@Override public void run() { try {// w w w .j a v a 2 s . c om MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpClient client = new HttpClient(connectionManager); HttpMethod method = new GetMethod(url); client.executeMethod(method); System.out.println(new String(method.getResponseBody())); } catch (IOException ex) { Logger.getLogger(HttpClientJob.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.zimbra.cs.service.UserServlet.java
public static Pair<Header[], byte[]> getRemoteResource(ZAuthToken authToken, String url) throws ServiceException { HttpMethod get = null; try {// w w w .j a v a2 s . co m Pair<Header[], HttpMethod> pair = doHttpOp(authToken, new GetMethod(url)); get = pair.getSecond(); return new Pair<Header[], byte[]>(pair.getFirst(), get.getResponseBody()); } catch (IOException x) { throw ServiceException.FAILURE("Can't read response body " + url, x); } finally { if (get != null) { get.releaseConnection(); } } }
From source file:com.kylinolap.common.restclient.RestClient.java
public void wipeCache(String type, String action, String name) throws IOException { String url = baseUrl + "/cache/" + type + "/" + name + "/" + action; HttpMethod get = new PutMethod(url); try {//www . ja v a 2s. c o m int code = client.executeMethod(get); String msg = Bytes.toString(get.getResponseBody()); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); } catch (HttpException ex) { throw new IOException(ex); } finally { get.releaseConnection(); } }
From source file:com.kylinolap.common.restclient.RestClient.java
public String getKylinProperties() throws IOException { String url = baseUrl + "/admin/config"; HttpMethod get = new GetMethod(url); try {//from w w w . jav a2 s . com int code = client.executeMethod(get); String msg = Bytes.toString(get.getResponseBody()); JSONObject obj = new JSONObject(msg); msg = obj.getString("config"); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); return msg; } catch (JSONException e) { throw new IOException("Error when parsing json response from REST"); } finally { get.releaseConnection(); } }
From source file:com.universalmind.core.components.mockup.loremipsum.SchmIpsumService.java
@Test public void testService() { String schmipsumSource = "bible"; Integer paragraphs = 2;/*from www . j ava 2s . com*/ String loremText = null; String url = null; url = "http://www.schmipsum.com/ipsum/" + schmipsumSource + "/" + (paragraphs * 100); HttpMethod method = null; HttpClient client = new HttpClient(); try { method = new GetMethod(url); int statusCode = client.executeMethod(method); byte[] responseBody = method.getResponseBody(); // Create a response handler //ResponseHandler<String> responseHandler = new BasicResponseHandler(); //String responseBody = httpclient.execute(httpget, responseHandler); loremText = parseBaseballIpsum(new String(responseBody)); Assert.assertNotNull(loremText); Assert.assertTrue(loremText.length() > 100); } catch (Exception jse) { //do nothing } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources //httpclient.getConnectionManager().shutdown(); if (method != null) { method.releaseConnection(); } } }