List of usage examples for org.apache.commons.httpclient HttpException getMessage
public String getMessage()
From source file:ehu.ned.DBpediaSpotlightClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;//www. ja va 2 s . c o 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) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. // // Deal with the response. // // Use caution: ensure correct character encoding and is not binary data InputStream responseBody = method.getResponseBodyAsStream(); response = IOUtils.toString(responseBody, "UTF-8"); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:com.bdaum.juploadr.uploadapi.smugrest.SmugmugMethod.java
public boolean execute() throws ProtocolException, CommunicationException { HttpMethodBase method = getMethod(); boolean rv = false; try {//www .j a v a2 s . co m int response = client.executeMethod(method); if (HttpStatus.SC_OK == response) { rv = parseResponse(method.getResponseBodyAsString()); } else { throw new CommunicationException(Messages.getString("juploadr.ui.error.bad.http.response", //$NON-NLS-1$ Activator.getStatusText(response))); } } catch (HttpException e) { throw new CommunicationException(e.getMessage(), e); } catch (IOException e) { throw new CommunicationException(e.getMessage(), e); } finally { method.releaseConnection(); } return rv; }
From source file:com.endpoint.lg.director.bridge.MasterApi.java
private StandardJsonNavigator sendRequest(String path) { String url = masterUrl + "/" + path; log.info("Hitting url: " + url); GetMethod method = new GetMethod(url); StandardJsonNavigator responseJson = null; try {/*from ww w .ja v a 2 s. co 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. String responseBody = new String(method.getResponseBody()); log.info(responseBody); responseJson = new StandardJsonNavigator(jsonMapper.parseObject(responseBody)); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } return responseJson; }
From source file:AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;/* www. ja va 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) { LOG.error("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) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:com.mythesis.userbehaviouranalysis.AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;/*from w ww.j ava 2s.c o 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) { LOG.error("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. InputStream responseBodyAsStream = method.getResponseBodyAsStream(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data StringWriter writer = new StringWriter(); IOUtils.copy(responseBodyAsStream, writer, "utf-8"); response = writer.toString(); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:net.sourceforge.fenixedu.presentationTier.Action.externalServices.PhoneValidationUtils.java
public boolean sendSMS(String number, String token) { number = number.replace(" ", ""); final String message = "Bem-vindo ao sistema Fenix. Introduza o codigo " + token + " . Obrigado!"; if (canRun()) { PostMethod method = new PostMethod(CIIST_SMS_GATEWAY_URL); method.addParameter(new NameValuePair("number", number)); method.addParameter(new NameValuePair("msg", message)); try {// ww w.j av a2s . c om CIIST_CLIENT.executeMethod(method); if (method.getStatusCode() != 200) { return false; } } catch (HttpException e) { // TODO Auto-generated catch block logger.error(e.getMessage(), e); return false; } catch (IOException e) { // TODO Auto-generated catch block logger.error(e.getMessage(), e); return false; } } else { logger.info("SMS to >" + number + "<: " + message); } return true; }
From source file:dk.dma.epd.common.prototype.communication.webservice.ShoreHttp.java
public void makeRequest() throws ShoreServiceException { // Make the request int resCode = -1; try {/* ww w . j av a2s. c o m*/ resCode = httpClient.executeMethod(method); } catch (HttpException e) { LOG.error("HTTP request failed with: " + e.getMessage()); throw new ShoreServiceException(ShoreServiceErrorCode.INTERNAL_ERROR); } catch (IOException e) { LOG.error("Failed to make HTTP connection: " + e.getMessage()); throw new ShoreServiceException(ShoreServiceErrorCode.NO_CONNECTION_TO_SERVER); } if (resCode != 200) { method.releaseConnection(); throw new ShoreServiceException(ShoreServiceErrorCode.SERVER_ERROR); } try { responseBody = method.getResponseBody(); int rawResSize = responseBody.length; // Check for GZip content encoding Header contentEncoding = method.getResponseHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().toUpperCase().indexOf("GZIP") >= 0) { responseBody = Compressor.decompress(responseBody); } LOG.debug("Received XML: " + new String(responseBody)); LOG.debug("Received XML size : " + responseBody.length); LOG.debug("Received raw XML size: " + rawResSize); } catch (IOException e) { LOG.error("Failed to read response body: " + e.getMessage()); throw new ShoreServiceException(ShoreServiceErrorCode.INVALID_RESPONSE); } method.releaseConnection(); }
From source file:net.kenware.langtrans.GoogleTranslater.java
/** * translates source string (sin) into a response using the source/target * languages.//from ww w.java 2 s.c o m * * @param sin String in * @param sourcelang source language (ISO3166 code) * @param targetlang target language (ISO3166 code) * @return returns a text string in the target language */ public String urlTranslate(String sin, String sourcelang, String targetlang) { String response = ""; String url = null; HttpClient client = new HttpClient(); HttpMethod method = null; String responsebody = null; try { String s = URLEncoder.encode(sin, "UTF-8"); url = "http://google.com/translate_t?hl=en&ie=UTF8&text=" + s + "&langpair=" + sourcelang + "%7C" + targetlang; client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); method = new GetMethod(url); method.setFollowRedirects(true); client.executeMethod(method); responsebody = method.getResponseBodyAsString(); method.releaseConnection(); response = extractResponse(responsebody); } catch (HttpException he) { System.err.println("Http error connecting to '" + url + "'"); System.err.println(he.getMessage()); } catch (IOException ioe) { System.err.println("Unable to connect to '" + url + "'"); } catch (Exception ex) { System.err.println("EXCEPTION: " + ex.getMessage()); } // end-try-catch return (response); }
From source file:com.gisgraphy.rest.RestClient.java
private InputStream executeMethod(HttpMethod httpMethod) throws RestClientException { int statusCode = -1; try {/*from w w w.ja v a 2 s . c o m*/ statusCode = executeAndCheckStatusCode(httpMethod); logger.info("URL " + httpMethod.getURI() + " returns http status :" + statusCode); return httpMethod.getResponseBodyAsStream(); } catch (HttpException e) { throw new RestClientException(statusCode, e.getMessage()); } catch (IOException e) { throw new RestClientException(statusCode, e.getMessage()); } }
From source file:ixa.pipe.ned.DBpediaSpotlightClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;//from w w w. jav a 2s . c om // 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) { LOG.error("Method failed: " + method.getStatusLine()); } // Read the response body. // // Deal with the response. // // Use caution: ensure correct character encoding and is not binary data InputStream responseBody = method.getResponseBodyAsStream(); response = StreamUtils.copyToString(responseBody, Charset.forName("UTF-8")); } catch (HttpException e) { LOG.error("Fatal protocol violation: " + e.getMessage()); throw new AnnotationException("Protocol error executing HTTP request.", e); } catch (IOException e) { LOG.error("Fatal transport error: " + e.getMessage()); LOG.error(method.getQueryString()); throw new AnnotationException("Transport error executing HTTP request.", e); } finally { // Release the connection. method.releaseConnection(); } return response; }