List of usage examples for org.apache.commons.httpclient HttpMethod execute
public abstract int execute(HttpState paramHttpState, HttpConnection paramHttpConnection) throws HttpException, IOException;
From source file:it.intecs.pisa.toolbox.util.URLReader.java
public static Object getURLContent(String host, String url, int port) throws Exception { HttpMethod method; try {//from ww w. j a v a 2s. co m method = new GetMethod(url); HttpConnection conn = new HttpConnection(host, port); String proxyHost; String proxyPort; if ((proxyHost = System.getProperty("http.proxyHost")) != null && (proxyPort = System.getProperty("http.proxyPort")) != null) { conn.setProxyHost(proxyHost); conn.setProxyPort(Integer.parseInt(proxyPort)); } conn.open(); method.execute(new HttpState(), conn); String inpTmp = method.getResponseBodyAsString(); Reader in = new InputStreamReader(method.getResponseBodyAsStream()); StringBuffer out = new StringBuffer(); char[] buffer = new char[1024]; for (int count = in.read(buffer); count >= 0; count = in.read(buffer)) { out.append(buffer, 0, count); } return out.toString(); } catch (Exception e) { e.printStackTrace(System.out); return null; } }
From source file:org.cauldron.tasks.HttpCall.java
/** * Running an HttpTask retrieves the path contents according to the task * attributes. POST body comes from the input. *///from ww w.jav a2s .com public Object run(Context context, Object input) throws TaskException { // For POST, body must be available as input. String body = null; if (!isGet) { body = (String) context.convert(input, String.class); if (body == null) throw new TaskException("HTTP POST input must be convertible to String"); } // Prepare request parameters. NameValuePair[] nvp = null; if (params != null && params.size() > 0) { nvp = new NameValuePair[params.size()]; int count = 0; for (Iterator entries = params.entrySet().iterator(); entries.hasNext();) { Map.Entry entry = (Map.Entry) entries.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); nvp[count++] = new NameValuePair(key, value); } } // Create the retrieval method and set parameters. // HttpMethod method; if (isGet) { GetMethod get = new GetMethod(); if (nvp != null) get.setQueryString(nvp); method = get; } else { PostMethod post = new PostMethod(); post.setRequestBody(body); if (nvp != null) post.addParameters(nvp); method = post; } // Make the call. method.setPath(path); HttpConnection connection = connectionManager.getConnection(config); try { connection.open(); method.execute(new HttpState(), connection); return method.getResponseBodyAsString(); } catch (HttpException e) { throw new TaskException(e); } catch (IOException e) { throw new TaskException(e); } finally { connection.close(); } }
From source file:org.mulgara.resolver.http.HttpContent.java
/** * Obtain a valid connection and follow redirects if necessary. * //from w w w .ja v a2s . com * @param methodType request the headders (HEAD) or body (GET) * @return valid connection method. Can be null. * @throws NotModifiedException if the content validates against the cache * @throws IOException if there's difficulty communicating with the web site */ private HttpMethod establishConnection(int methodType) throws IOException, NotModifiedException { if (logger.isDebugEnabled()) logger.debug("Establishing connection"); HttpMethod method = getConnectionMethod(methodType); assert method != null; Header header = null; /* // Add cache validation headers to the request if (lastModifiedMap.containsKey(httpUri)) { String lastModified = (String) lastModifiedMap.get(httpUri); assert lastModified != null; method.addRequestHeader("If-Modified-Since", lastModified); } if (eTagMap.containsKey(httpUri)) { String eTag = (String) eTagMap.get(httpUri); assert eTag != null; method.addRequestHeader("If-None-Match", eTag); } */ // Make the request if (logger.isDebugEnabled()) logger.debug("Executing HTTP request"); connection.open(); method.execute(state, connection); if (logger.isDebugEnabled()) { logger.debug("Executed HTTP request, response code " + method.getStatusCode()); } // Interpret the response header if (method.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) { // cache has been validated throw new NotModifiedException(httpUri); } else if (!isValidStatusCode(method.getStatusCode())) { throw new UnknownHostException("Unable to obtain connection to " + httpUri + ". Returned status code " + method.getStatusCode()); } else { // has a redirection been issued int numberOfRedirection = 0; while (isRedirected(method.getStatusCode()) && numberOfRedirection <= MAX_NO_REDIRECTS) { // release the existing connection method.releaseConnection(); //attempt to follow the redirects numberOfRedirection++; // obtain the new location header = method.getResponseHeader("location"); if (header != null) { try { initialiseSettings(new URL(header.getValue())); if (logger.isInfoEnabled()) { logger.info("Redirecting to " + header.getValue()); } // attempt a new connection to this location method = getConnectionMethod(methodType); connection.open(); method.execute(state, connection); if (!isValidStatusCode(method.getStatusCode())) { throw new UnknownHostException( "Unable to obtain connection to " + " the redirected site " + httpUri + ". Returned status code " + method.getStatusCode()); } } catch (URISyntaxException ex) { throw new IOException( "Unable to follow redirection to " + header.getValue() + " Not a valid URI"); } } else { throw new IOException("Unable to obtain redirecting detaild from " + httpUri); } } } // Update metadata about the cached document Header lastModifiedHeader = method.getResponseHeader("Last-Modified"); if (lastModifiedHeader != null) { logger.debug(lastModifiedHeader.toString()); assert lastModifiedHeader.getElements().length >= 1; assert lastModifiedHeader.getElements()[0].getName() != null; assert lastModifiedHeader.getElements()[0].getName() instanceof String; // previous code: added to cache } Header eTagHeader = method.getResponseHeader("Etag"); if (eTagHeader != null) { logger.debug(eTagHeader.toString()); assert eTagHeader.getElements().length >= 1; assert eTagHeader.getElements()[0].getName() != null; assert eTagHeader.getElements()[0].getName() instanceof String; // previous code: added to cache } return method; }