List of usage examples for org.apache.commons.httpclient HttpException getMessage
public String getMessage()
From source file:net.neurowork.cenatic.centraldir.workers.XMLPushTest.java
private void pushOrganization(String token) { PostMethod post = new PostMethod(restUrl.getPushOrganizationUrl()); NameValuePair tokenParam = new NameValuePair("token", token); NameValuePair[] params = new NameValuePair[] { tokenParam }; post.addRequestHeader("Accept", "application/xml"); post.setQueryString(params);/* ww w.jav a 2s.co m*/ post.setRequestBody(XmlPushCreator.getInstance().getPushXml(organizacion)); try { int result = httpclient.executeMethod(post); logger.info("Response status code: " + result); logger.info("Response body: "); logger.info(post.getResponseBodyAsString()); } catch (HttpException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } finally { post.releaseConnection(); } }
From source file:br.org.acessobrasil.silvinha.util.versoes.VerificadorDeVersoes.java
public boolean verificarVersao(Versao versaoCliente) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); NameValuePair param = new NameValuePair("param", "check"); method.setRequestBody(new NameValuePair[] { param }); DefaultHttpMethodRetryHandler retryHandler = null; retryHandler = new DefaultHttpMethodRetryHandler(0, false); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); try {//from w ww .ja v a2 s. c om //System.out.println(Silvinha.VERIFICADOR_VERSOES+client.getState().toString()); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); String rb = new String(responseBody).trim(); Versao versaoServidor = null; try { versaoServidor = new Versao(rb); } catch (NumberFormatException nfe) { return false; } if (versaoCliente.compareTo(versaoServidor) < 0) { AtualizadorDeVersoes av = new AtualizadorDeVersoes(url); return av.confirmarAtualizacao(); } else { return false; } } catch (HttpException he) { log.error("Fatal protocol violation: " + he.getMessage(), he); return false; } catch (IOException ioe) { log.error("Fatal transport error: " + ioe.getMessage(), ioe); return false; } catch (Exception e) { return false; } finally { //Release the connection. method.releaseConnection(); } }
From source file:net.neurowork.cenatic.centraldir.workers.XMLPushTest.java
@Test public void testPushOrganizacion() { GetMethod get = new GetMethod(restUrl.getTokenUrl()); get.addRequestHeader("Accept", "application/xml"); NameValuePair userParam = new NameValuePair(USERNAME_PARAM, user); NameValuePair passwordParam = new NameValuePair(PASSWORD_PARAM, password); NameValuePair[] params = new NameValuePair[] { userParam, passwordParam }; get.setQueryString(params);//from w ww . j a v a2 s.com try { int result = httpclient.executeMethod(get); logger.info("Response status code: " + result); logger.info("Response body: "); logger.info(get.getResponseBodyAsString()); String token = XMLRestWorker.parseToken(get.getResponseBodyAsString()); if (token != null) { pushOrganization(token); } } catch (HttpException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } finally { get.releaseConnection(); } }
From source file:br.org.acessobrasil.silvinha.autenticador.AutenticadorConector.java
public boolean autenticarLogin(Login login) { boolean autenticado = false; HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); NameValuePair x1 = new NameValuePair("x1", login.getUser()); NameValuePair x2 = new NameValuePair("x2", login.getPass()); method.setRequestBody(new NameValuePair[] { x1, x2 }); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try {//ww w .j a v a 2 s.c o m int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); String rb = new String(responseBody).trim(); // Deal with the response. if (rb.startsWith("OK")) { autenticado = true; String[] rbLines = rb.split("\n"); Token.setUrl(rbLines[1]); } } catch (HttpException he) { log.error("Fatal protocol violation: " + he.getMessage(), he); } catch (IOException ioe) { log.error("Fatal transport error: " + ioe.getMessage(), ioe); } finally { /* * Release the connection. */ method.releaseConnection(); } return autenticado; }
From source file:com.lp.client.frame.component.phone.HttpPhoneDialer.java
protected void dialImpl(String number) throws ExceptionLP { myLogger.info("Dialing <" + number + "> ..."); GetMethod method = getHttpMethod(number); try {// ww w. ja va 2s .c o m prepareClient(client, method); int status = client.executeMethod(method); myLogger.info("Dialing done with HTTP Status: <" + status + ">"); if (status != HttpStatus.SC_OK) { byte[] responseBody = method.getResponseBody(); String s = new String(responseBody); myLogger.debug("Done dialing with response <" + s + ">"); } } catch (HttpException ex) { myLogger.debug("Got HttpException <" + ex.getMessage() + ">"); throw new ExceptionLP(EJBExceptionLP.FEHLER_TELEFON_WAHLVORGANG, ex.getMessage(), ex); } catch (IOException ex) { myLogger.debug("Got IOException <" + ex.getMessage() + ">"); throw new ExceptionLP(EJBExceptionLP.FEHLER_TELEFON_WAHLVORGANG, ex.getMessage(), ex); } finally { method.releaseConnection(); } }
From source file:net.neurowork.cenatic.centraldir.workers.xml.AsociacionXmlImporter.java
private Asociacion buscarAsociacion(int code) { HttpClient httpclient = XMLRestWorker.getHttpClient(); GetMethod get = new GetMethod(restUrl.getAssociationUrl()); get.addRequestHeader("Accept", "application/xml"); Asociacion ret = null;//from ww w . ja v a 2s.c o m try { int result = httpclient.executeMethod(get); logger.info("Response status code: " + result); String xmlString = get.getResponseBodyAsString(); logger.info("Response body: " + xmlString); ret = importAsociacion(xmlString, code); } catch (HttpException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } catch (ParserConfigurationException e) { logger.error(e.getMessage()); } catch (SAXException e) { logger.error(e.getMessage()); } finally { get.releaseConnection(); } return ret; }
From source file:br.com.edu.dbpediaspotlight.AnnotationClient.java
public String request(HttpMethod method) throws AnnotationException { String response = null;//from w w w . j a va 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. 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.discogs.api.webservice.impl.HttpClientWebService.java
@Override public Resp doGet(String url) throws WebServiceException { HttpMethod method = new GZipCapableGetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); method.setDoAuthentication(true);//from w w w . java2 s . c om try { // execute the method int statusCode = this.httpClient.executeMethod(method); if (logger.isDebugEnabled()) { logger.debug(method.getResponseBodyAsString()); } switch (statusCode) { case HttpStatus.SC_OK: return createResp(method.getResponseBodyAsStream()); case HttpStatus.SC_NOT_FOUND: throw new ResourceNotFoundException("Resource not found.", method.getResponseBodyAsString()); case HttpStatus.SC_BAD_REQUEST: throw new RequestException(method.getResponseBodyAsString()); case HttpStatus.SC_FORBIDDEN: throw new AuthorizationException(method.getResponseBodyAsString()); case HttpStatus.SC_UNAUTHORIZED: throw new AuthorizationException(method.getResponseBodyAsString()); default: String em = "web service returned unknown status '" + statusCode + "', response was: " + method.getResponseBodyAsString(); logger.error(em); throw new WebServiceException(em); } } catch (HttpException e) { logger.error("Fatal protocol violation: " + e.getMessage()); throw new WebServiceException(e.getMessage(), e); } catch (IOException e) { logger.error("Fatal transport error: " + e.getMessage()); throw new WebServiceException(e.getMessage(), e); } finally { method.releaseConnection(); } }
From source file:eu.eco2clouds.accounting.bonfire.BFClientAccountingImpl.java
private String getMethod(String url, Boolean exception) { // Create an instance of HttpClient. HttpClient client = getHttpClient(); logger.debug("Connecting to: " + url); // Create a method instance. GetMethod method = new GetMethod(url); setHeaders(method);//from ww w .j a va2s .co m // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); String response = ""; try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { //TODO test for this case... logger.warn( "get managed experiments information... : " + url + " failed: " + method.getStatusLine()); } else { // Read the response body. byte[] responseBody = method.getResponseBody(); response = new String(responseBody); } } catch (HttpException e) { logger.warn("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); exception = true; } catch (IOException e) { logger.warn("Fatal transport error: " + e.getMessage()); e.printStackTrace(); exception = true; } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:dk.dma.epd.common.prototype.shoreservice.RouteHttp.java
public void makeRequest() throws Exception { // Make the request int resCode = -1; try {// ww w. ja v a 2s . c o m // System.out.println("Trying to connect to server"); resCode = httpClient.executeMethod(method); // System.out.println("Connected!"); } catch (HttpException e) { LOG.error("Failed to make HTTP connection: " + e.getMessage()); 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()); System.out.println("Failed: " + e.getMessage()); // throw new // ShoreServiceException(ShoreServiceErrorCode.NO_CONNECTION_TO_SERVER); } // System.out.println(resCode); if (resCode != 200) { method.releaseConnection(); } try { responseBody = method.getResponseBodyAsString(); // System.out.println(responseBody); } catch (IOException e) { LOG.error("Failed to read response body: " + e.getMessage()); // throw new // ShoreServiceException(ShoreServiceErrorCode.INVALID_RESPONSE); } method.releaseConnection(); }