List of usage examples for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER
String RETRY_HANDLER
To view the source code for org.apache.commons.httpclient.params HttpMethodParams RETRY_HANDLER.
Click Source Link
From source file:org.openremote.beehive.utils.LIRCrawler.java
/** * Gets the html body.// ww w. j a v a 2s.c om * * @param url * the url * * @return the html body */ private static String getHtmlBody(String url) { String responseBody = ""; GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { LOGGER.error("Method failed: " + getMethod.getStatusLine()); } BufferedReader bufferIn = new BufferedReader( new InputStreamReader(getMethod.getResponseBodyAsStream())); StringBuffer sb = new StringBuffer(); char[] buf = new char[1024 * 1024]; int len; while ((len = bufferIn.read(buf)) > 0) { sb.append(buf, 0, len); } responseBody = sb.toString(); } catch (HttpException e) { LOGGER.error("Please check your provided http address " + url, e); return null; } catch (IOException e) { LOGGER.error("Occur the network exception, maybe the url [" + url + "] is unreachable.", e); return null; } finally { getMethod.releaseConnection(); } return responseBody; }
From source file:org.opentides.util.UrlUtil.java
/** * Returns the HTML code of the original engine. Takes the URL to connect to * the engine. Also takes encoding type that overrides default if not null * "UTF8" is typical encoding type/*from w ww . j a va 2 s . com*/ * * @param queryURL * - URL of engine to retrieve * @param request * - request object * @param param * - additional parameters * - Valid parameters are: * - methodName - Either "POST" or "GET". Default is "POST" * - forwardCookie - if true, will forward cookies found on request object * - IPAddress - if specified, this IP will be used for the request * */ public static final UrlResponseObject getPage(final String queryURL, final HttpServletRequest request, final Map<String, Object> param) { // determine if get or post method HttpMethodBase httpMethodBase; Boolean forwardCookie = false; InetAddress IPAddress = null; if (param != null) { if (param.get("forwardCookie") != null) forwardCookie = (Boolean) param.get("forwardCookie"); if (param.get("IPAddress") != null) { String IPString = (String) param.get("IPAddress"); if (!StringUtil.isEmpty(IPString)) { IPAddress = convertIPString(IPString); } } } if (param != null && "GET".equals((String) param.get("methodName"))) { httpMethodBase = new GetMethod(queryURL); } else { httpMethodBase = new PostMethod(queryURL); } try { // declare the connection objects HttpClient client = new HttpClient(); HostConfiguration hostConfig = new HostConfiguration(); String userAgent = request.getHeader("User-Agent"); // for debugging if (_log.isDebugEnabled()) _log.debug("Retrieving page from " + queryURL); // initialize the connection settings client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT); client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); httpMethodBase.addRequestHeader("accept", "*/*"); httpMethodBase.addRequestHeader("accept-language", "en-us"); httpMethodBase.addRequestHeader("user-agent", userAgent); if (forwardCookie) { // get cookies from request Cookie[] cookies = request.getCookies(); String cookieString = ""; for (Cookie c : cookies) { cookieString += c.getName() + "=" + c.getValue() + "; "; } // forward cookies to httpMethod httpMethodBase.setRequestHeader("Cookie", cookieString); } if (IPAddress != null) { hostConfig.setLocalAddress(IPAddress); } // Setup for 3 retry httpMethodBase.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // now let's retrieve the data client.executeMethod(hostConfig, httpMethodBase); // Read the response body. UrlResponseObject response = new UrlResponseObject(); response.setResponseBody(httpMethodBase.getResponseBody()); Header contentType = httpMethodBase.getResponseHeader("Content-Type"); if (contentType != null) response.setResponseType(contentType.getValue()); else response.setResponseType(Widget.TYPE_HTML); return response; } catch (Exception ex) { _log.error("Failed to request from URL: [" + queryURL + "]", ex); return null; } finally { try { httpMethodBase.releaseConnection(); } catch (Exception ignored) { } } }
From source file:org.opentides.util.WidgetUtil.java
/** * Returns the HTML code of the original engine. Takes the URL to connect to * the engine. Also takes encoding type that overrides default if not null * "UTF8" is typical encoding type/*www. j a va2 s . c o m*/ * * @param queryURL * - URL of engine to retrieve * @param request * - request object * @param param * - additional parameters * - Valid parameters are: * - methodName - Either "POST" or "GET". Default is "POST" * - forwardCookie - if true, will forward cookies found on request object * - IPAddress - if specified, this IP will be used for the request * */ public static final UrlResponseObject getPage(final String queryURL, final HttpServletRequest request, final Map<String, Object> param) { // determine if get or post method HttpMethodBase httpMethodBase; Boolean forwardCookie = false; InetAddress IPAddress = null; if (param != null) { if (param.get("forwardCookie") != null) forwardCookie = (Boolean) param.get("forwardCookie"); if (param.get("IPAddress") != null) { String IPString = (String) param.get("IPAddress"); if (!StringUtil.isEmpty(IPString)) { IPAddress = UrlUtil.convertIPString(IPString); } } } if (param != null && "GET".equals((String) param.get("methodName"))) { httpMethodBase = new GetMethod(queryURL); } else { httpMethodBase = new PostMethod(queryURL); } try { // declare the connection objects HttpClient client = new HttpClient(); HostConfiguration hostConfig = new HostConfiguration(); String userAgent = request.getHeader("User-Agent"); // for debugging if (_log.isDebugEnabled()) _log.debug("Retrieving page from " + queryURL); // initialize the connection settings client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT); client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true); httpMethodBase.addRequestHeader("accept", "*/*"); httpMethodBase.addRequestHeader("accept-language", "en-us"); httpMethodBase.addRequestHeader("user-agent", userAgent); if (forwardCookie) { // get cookies from request Cookie[] cookies = request.getCookies(); String cookieString = ""; for (Cookie c : cookies) { cookieString += c.getName() + "=" + c.getValue() + "; "; } // forward cookies to httpMethod httpMethodBase.setRequestHeader("Cookie", cookieString); } if (IPAddress != null) { hostConfig.setLocalAddress(IPAddress); } // Setup for 3 retry httpMethodBase.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); // now let's retrieve the data client.executeMethod(hostConfig, httpMethodBase); // Read the response body. UrlResponseObject response = new UrlResponseObject(); response.setResponseBody(httpMethodBase.getResponseBody()); Header contentType = httpMethodBase.getResponseHeader("Content-Type"); if (contentType != null) response.setResponseType(contentType.getValue()); else response.setResponseType("html"); return response; } catch (Exception ex) { _log.error("Failed to request from URL: [" + queryURL + "]", ex); return null; } finally { try { httpMethodBase.releaseConnection(); } catch (Exception ignored) { } } }
From source file:org.oryxeditor.server.TBPMServlet.java
private String sendRequest(File img, String fileName) { HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000); PostMethod method = new PostMethod(TBPM_RECOGNITION_URL); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); String response = ""; try {/*from ww w .j av a2s . c o m*/ Part[] parts = { new FilePart(img.getName(), img) }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); client.executeMethod(method); //TODO handle response status response = method.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } method.releaseConnection(); return response; }
From source file:org.panlab.software.fci.uop.UoPGWClient.java
/** * It makes a POST towards the gateway//from w w w.j av a 2s . co m * @author ctranoris * @param resourceInstance sets the name of the resource Instance, e.g.: uop.rubis_db-27 * @param ptmAlias sets the name of the provider URI, e.g.: uop * @param content sets the name of the content; send in utf8 */ public boolean POSTExecute(String resourceInstance, String ptmAlias, String content) { boolean status = false; System.out.println("content body=" + "\n" + content); HttpClient client = new HttpClient(); String tgwcontent = content; // resource instance is like uop.rubis_db-6 so we need to make it like // this /uop/uop.rubis_db-6 String ptm = ptmAlias; String url = uopGWAddress + "/" + ptm + "/" + resourceInstance; System.out.println("Request: " + url); // Create a method instance. PostMethod post = new PostMethod(url); post.setRequestHeader("User-Agent", userAgent); post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Provide custom retry handler is necessary post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); //HttpMethodParams. RequestEntity requestEntity = null; try { requestEntity = new StringRequestEntity(tgwcontent, "application/x-www-form-urlencoded", "utf-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } post.setRequestEntity(requestEntity); try { // Execute the method. int statusCode = client.executeMethod(post); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + post.getStatusLine()); } // Deal with the response. // Use caution: ensure correct character encoding and is not binary // data // print the status and response InputStream responseBody = post.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); System.out.println("Response body=" + "\n" + convertStreamToString(response_stream)); response_stream.reset(); // System.out.println("for address: " + url + " the response is:\n " // + post.getResponseBodyAsString()); status = true; } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); return false; } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); return false; } finally { // Release the connection. post.releaseConnection(); } return status; }
From source file:org.parosproxy.paros.network.HttpSender.java
/** * Sets the maximum number of retries of an unsuccessful request caused by I/O errors. * <p>//from w w w.ja v a 2s. com * The default number of retries is 3. * * @param retries the number of retries * @throws IllegalArgumentException if {@code retries} is negative. * @since 2.4.0 */ public void setMaxRetriesOnIOError(int retries) { if (retries < 0) { throw new IllegalArgumentException("Parameter retries must be greater or equal to zero."); } HttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(retries, false); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); clientViaProxy.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); }
From source file:org.paxml.bean.HttpTag.java
/** * {@inheritDoc}//from ww w .ja va2 s .c om */ @Override protected Object doInvoke(Context context) throws Exception { String lowUrl = url.toLowerCase(); if (!lowUrl.startsWith("http://") && !lowUrl.startsWith("https://")) { url = "http://" + url; } HttpClient client = new HttpClient(); final HttpMethodBase m; if ("post".equalsIgnoreCase(method)) { m = setPostBody(new PostMethod(url)); } else if ("get".equalsIgnoreCase(method)) { m = new GetMethod(url); } else { throw new PaxmlRuntimeException("Unknown method: " + method); } setHeader(m); setQueryString(m); // Provide custom retry handler is necessary m.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(maxRetry, false)); onBeforeSend(m); // method.setr try { // Execute the method. final int statusCode = client.executeMethod(m); if (responseless) { return statusCode; } // Read the response body. Map<String, Object> result = new HashMap<String, Object>(); result.put("code", statusCode); result.put("body", m.getResponseBodyAsString()); result.put("all", m); return result; } finally { // Release the connection. m.releaseConnection(); } }
From source file:org.paxml.bean.SoapTag.java
/** * {@inheritDoc}/*from ww w . j a v a 2s. c o m*/ */ @Override protected Object doInvoke(Context context) throws Exception { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out); // method.setRequestHeader(headerName, headerValue); method.setRequestBody(out.toString("UTF-8")); method.setRequestHeader("Content-Type", "text/xml;charset=UTF-8"); if (headers != null) { Map<?, ?> hd = (Map<?, ?>) this.headers; for (Map.Entry<?, ?> entry : hd.entrySet()) { Object obj = entry.getValue(); if (obj != null) { method.setRequestHeader(entry.getKey().toString(), obj.toString()); } } } // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(RETRY, false)); // method.setr try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new PaxmlRuntimeException("Http post failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary // data return read(new ByteArrayInputStream(responseBody)); } finally { // Release the connection. method.releaseConnection(); } }
From source file:org.portletbridge.portlet.DefaultHttpClientTemplate.java
/** * //from w w w.j ava 2 s . com */ private DefaultHttpClientTemplate(Builder b) { retryCount = b.retryCount; connectionTimeout = b.connectionTimeout; readTimeoutMillis = b.readTimeoutMillis; httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); httpClient.getHttpConnectionManager().getParams().setSoTimeout(readTimeoutMillis); HttpMethodRetryHandler retryhandler = new HttpMethodRetryHandler() { public boolean retryMethod(final HttpMethod method, final IOException exception, int executionCount) { if (executionCount >= retryCount) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (exception instanceof SocketException) { // Retry if the server reset connection on us return true; } if (exception instanceof SocketTimeoutException) { // Retry if the read timed out return true; } if (!method.isRequestSent()) { // Retry if the request has not been sent fully or // if it's OK to retry methods that have been sent return true; } // otherwise do not retry return false; } }; httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler); }
From source file:org.qedeq.base.io.UrlUtility.java
/** * Make local copy of a http accessable URL. This method uses apaches HttpClient, * but it dosn't work under webstart with proxy configuration. If we don't use this * method, the apache commons-httpclient library can be removed * * @param url Save this URL. * @param f Save into this file. An existing file is overwritten. * @param proxyHost Use this proxy host. * @param proxyPort Use this port at proxy host. * @param nonProxyHosts This are hosts not to be proxied. * @param connectTimeout Connection timeout. * @param readTimeout Read timeout. * @param listener Here completion events are fired. * @throws IOException Saving failed. *///from ww w . ja v a 2 s. c om private static void saveQedeqFromWebToBufferApache(final String url, final File f, final String proxyHost, final String proxyPort, final String nonProxyHosts, final int connectTimeout, final int readTimeout, final LoadingListener listener) throws IOException { final String method = "saveQedeqFromWebToBufferApache()"; Trace.begin(CLASS, method); // Create an instance of HttpClient. HttpClient client = new HttpClient(); // set proxy properties according to kernel configuration (if not webstarted) if (!IoUtility.isWebStarted() && proxyHost != null && proxyHost.length() > 0) { final String pHost = proxyHost; int pPort = 80; if (proxyPort != null) { try { pPort = Integer.parseInt(proxyPort); } catch (RuntimeException e) { Trace.fatal(CLASS, method, "proxy port not numeric: " + proxyPort, e); } } if (pHost.length() > 0) { client.getHostConfiguration().setProxy(pHost, pPort); } } // Create a method instance. GetMethod httpMethod = new GetMethod(url); try { // Provide custom retry handler is necessary httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); httpMethod.getParams().setSoTimeout(connectTimeout); // Throws IOException on TimeOut. int statusCode = client.executeMethod(httpMethod); if (statusCode != HttpStatus.SC_OK) { throw new FileNotFoundException("Problems loading: " + url + "\n" + httpMethod.getStatusLine()); } // Read the response body. byte[] responseBody = httpMethod.getResponseBody(); IoUtility.createNecessaryDirectories(f); IoUtility.saveFileBinary(f, responseBody); listener.loadingCompletenessChanged(1); } finally { // Release the connection. httpMethod.releaseConnection(); Trace.end(CLASS, method); } }