public static void main(String[] args) { HttpClient client = new HttpClient(); // Create an instance of HttpClient. GetMethod method = new GetMethod(url); // Create a method instance. method .getParams() .setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler( 3, false)); // Provide custom retry handler is necessary try { int statusCode = client.executeMethod(method); // Execute the method. if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } byte[] responseBody = method.getResponseBody(); // Read the response body. System.out.println( new String( responseBody)); // Deal with the response.// Use caution: ensure correct character // encoding and is not binary data } 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 { method.releaseConnection(); // Release the connection. } }
public static Data getHTTPData( HttpServletRequest req, HttpServletResponse res, String surl, long since) throws DataSourceException, IOException { int tries = 1; // timeout msecs of time we're allowed in this routine // we must return or throw an exception. 0 means infinite. int timeout = mTimeout; if (req != null) { String timeoutParm = req.getParameter("timeout"); if (timeoutParm != null) { timeout = Integer.parseInt(timeoutParm); } } long t1 = System.currentTimeMillis(); long elapsed = 0; if (surl == null) { surl = getURL(req); } while (true) { String m = null; long tout; if (timeout > 0) { tout = timeout - elapsed; if (tout <= 0) { throw new InterruptedIOException( /* (non-Javadoc) * @i18n.test * @org-mes=p[0] + " timed out" */ org.openlaszlo.i18n.LaszloMessages.getMessage( HTTPDataSource.class.getName(), "051018-194", new Object[] {surl})); } } else { tout = 0; } try { HttpData data = getDataOnce(req, res, since, surl, 0, (int) tout); if (data.code >= 400) { data.release(); throw new DataSourceException(errorMessage(data.code)); } return data; } catch (HttpRecoverableException e) { // This type of exception should be retried. if (tries++ > mMaxRetries) { throw new InterruptedIOException( /* (non-Javadoc) * @i18n.test * @org-mes="too many retries, exception: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage( HTTPDataSource.class.getName(), "051018-217", new Object[] {e.getMessage()})); } mLogger.warn( /* (non-Javadoc) * @i18n.test * @org-mes="retrying a recoverable exception: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage( HTTPDataSource.class.getName(), "051018-226", new Object[] {e.getMessage()})); } catch (HttpException e) { String msg = /* (non-Javadoc) * @i18n.test * @org-mes="HttpException: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage( HTTPDataSource.class.getName(), "051018-235", new Object[] {e.getMessage()}); throw new IOException( /* (non-Javadoc) * @i18n.test * @org-mes="HttpException: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage( HTTPDataSource.class.getName(), "051018-235", new Object[] {e.getMessage()})); } catch (IOException e) { try { Class ssle = Class.forName("javax.net.ssl.SSLException"); if (ssle.isAssignableFrom(e.getClass())) { throw new DataSourceException( /* (non-Javadoc) * @i18n.test * @org-mes="SSL exception: " + p[0] */ org.openlaszlo.i18n.LaszloMessages.getMessage( HTTPDataSource.class.getName(), "051018-256", new Object[] {e.getMessage()})); } } catch (ClassNotFoundException cfne) { } throw e; } long t2 = System.currentTimeMillis(); elapsed = (t2 - t1); } }