Example usage for org.apache.commons.httpclient HttpMethod recycle

List of usage examples for org.apache.commons.httpclient HttpMethod recycle

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod recycle.

Prototype

public abstract void recycle();

Source Link

Usage

From source file:com.discursive.jccook.httpclient.SSLExample.java

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    String url = "https://www.amazon.com/gp/flex/sign-in.html";
    HttpMethod method = new GetMethod(url);
    client.executeMethod(method);//from  w w w . j a  v  a  2s . c  om
    String response = method.getResponseBodyAsString();
    System.out.println(response);
    method.releaseConnection();
    method.recycle();
}

From source file:com.discursive.jccook.httpclient.CustomSSLExample.java

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    String url = "https://pericles.symbiont.net/jccook";

    ProtocolSocketFactory socketFactory = new EasySSLProtocolSocketFactory();
    Protocol https = new Protocol("https", socketFactory, 443);
    Protocol.registerProtocol("https", https);

    HttpMethod method = new GetMethod(url);
    client.executeMethod(method);/* w ww  .  j  ava 2s.  co  m*/
    String response = method.getResponseBodyAsString();
    System.out.println(response);
    method.releaseConnection();
    method.recycle();
}

From source file:com.discursive.jccook.httpclient.DebuggingExample.java

public static void main(String[] args) throws HttpException, IOException {

    // Configure Logging
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");

    HttpClient client = new HttpClient();
    String url = "http://www.discursive.com/jccook/";
    HttpMethod method = new GetMethod(url);
    client.executeMethod(method);/*from www  . ja  va2 s  .com*/
    String response = method.getResponseBodyAsString();

    System.out.println(response);
    method.releaseConnection();
    method.recycle();
}

From source file:com.discursive.jccook.httpclient.GetExample.java

public static void main(String[] args) {
    HttpClient client = new HttpClient();
    String url = "http://www.discursive.com/jccook/";
    HttpMethod method = new GetMethod(url);

    try {//from w w  w  . j a  v  a  2  s .c o m
        client.executeMethod(method);
        String response = method.getResponseBodyAsString();

        System.out.println(response);
    } catch (HttpException he) {
        System.out.println("HTTP Problem: " + he.getMessage());
    } catch (IOException ioe) {
        System.out.println("IO Exeception: " + ioe.getMessage());
    } finally {
        method.releaseConnection();
        method.recycle();
    }
}

From source file:ExampleP2PHttpClient.java

public static void main(String[] args) {
    // initialize JXTA
    try {/* w  w  w .j ava2  s  . c o m*/
        // sign in and initialize the JXTA network; profile this peer and create it
        // if it doesn't exist
        P2PNetwork.signin("clientpeer", "clientpeerpassword", "TestNetwork", true);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    // register the P2P socket protocol factory
    Protocol jxtaHttp = new Protocol("p2phttp", new P2PProtocolSocketFactory(), 80);
    Protocol.registerProtocol("p2phttp", jxtaHttp);

    //create a singular HttpClient object
    HttpClient client = new HttpClient();

    //establish a connection within 50 seconds
    client.setConnectionTimeout(50000);

    String url = System.getProperty("url");
    if (url == null || url.equals("")) {
        System.out.println("You must provide a URL to access.  For example:");
        System.out.println("ant example-webclient-run -D--url=p2phttp://www.somedomain.foo");

        System.exit(1);
    }
    System.out.println("Connecting to " + url + "...");

    HttpMethod method = null;

    //create a method object
    method = new GetMethod(url);
    method.setFollowRedirects(true);
    method.setStrictMode(false);
    //} catch (MalformedURLException murle) {
    //    System.out.println("<url> argument '" + url
    //            + "' is not a valid URL");
    //    System.exit(-2);
    //}

    //execute the method
    String responseBody = null;
    try {
        client.executeMethod(method);
        responseBody = method.getResponseBodyAsString();
    } catch (HttpException he) {
        System.err.println("Http error connecting to '" + url + "'");
        System.err.println(he.getMessage());
        System.exit(-4);
    } catch (IOException ioe) {
        System.err.println("Unable to connect to '" + url + "'");
        System.exit(-3);
    }

    //write out the request headers
    System.out.println("*** Request ***");
    System.out.println("Request Path: " + method.getPath());
    System.out.println("Request Query: " + method.getQueryString());
    Header[] requestHeaders = method.getRequestHeaders();
    for (int i = 0; i < requestHeaders.length; i++) {
        System.out.print(requestHeaders[i]);
    }

    //write out the response headers
    System.out.println("*** Response ***");
    System.out.println("Status Line: " + method.getStatusLine());
    Header[] responseHeaders = method.getResponseHeaders();
    for (int i = 0; i < responseHeaders.length; i++) {
        System.out.print(responseHeaders[i]);
    }

    //write out the response body
    System.out.println("*** Response Body ***");
    System.out.println(responseBody);

    //clean up the connection resources
    method.releaseConnection();
    method.recycle();

    System.exit(0);
}

From source file:com.discursive.jccook.httpclient.RedirectExample.java

private static void executeMethod(HttpClient client, HttpMethod method) throws IOException, HttpException {
    client.executeMethod(method);// w  w w . j  ava2s .co m
    System.out.println("Response Code: " + method.getStatusCode());
    String response = method.getResponseBodyAsString();
    System.out.println(response);
    method.releaseConnection();
    method.recycle();
}

From source file:edu.pitt.dbmi.facebase.hd.HumanDataController.java

/** http web client class used for communicating with Hub 
 * @param action String is one of status, update, event
 * @param signal String is a timeInSeconds or QueueID or eventCode
 * for action=status, signal is "0", number-of-seconds-until-back, 
 * for action=update, signal is qid.hash
 * for action=event, signal "1" means mysql server is unavailable.
 * uses getOTP() method for URL construction
 *//* w w  w  .  ja  v a 2  s.  c o m*/
public static void httpGetter(String action, String signal) {
    String oneTimePassword = (new Long(getOTP())).toString();
    String url = hubURL + responseTrigger + oneTimePassword + "/";
    if (action == "update") {
        url += action + "/" + signal;
    } else if (action == "status") {
        long time = System.currentTimeMillis() / 1000L;
        time += Long.parseLong(signal);
        Long timeLong;
        timeLong = new Long(time);
        url += action + "/" + timeLong.toString();
    } else if (action == "event") {
        url += action + "/" + signal;
    } else {
    }
    HttpClient webClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    try {
        webClient.executeMethod(httpMethod);
        String response = httpMethod.getResponseBodyAsString();
    } catch (HttpException httpe) {
    } catch (IOException ioe) {
    } finally {
        httpMethod.releaseConnection();
        httpMethod.recycle();
    }
}

From source file:com.discursive.jccook.httpclient.ConditionalGetExample.java

public void start() throws HttpException, IOException {

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod("http://www.apache.org");

    for (int i = 0; i < 3; i++) {
        setHeaders(method);//  w  w  w  .  j  av a2s  .c  o m
        client.executeMethod(method);
        processResults(method);
        method.releaseConnection();
        method.recycle();
    }
}

From source file:autohit.call.modules.SimpleHttpModule.java

/**
 * Start method. It will set the target address for the client, as well as
 * clearing any state./*from  ww w.  j  a  v  a  2s  .  co  m*/
 * 
 * @param url
 *            the Url path, not to include protocol, address, and port (ie.
 *            "/goats/index.html").
 * @return the data from the page as a String
 * @throws CallException
 */
private String get(String url) throws CallException {

    if (started == false) {
        throw buildException("module:SimpleHttp:Tried to get when a session wasn't started.",
                CallException.CODE_MODULE_FAULT);
    }

    String result = null;

    // Construct our method.
    HttpMethod method = new GetMethod(url);
    method.setFollowRedirects(true);
    method.setStrictMode(false);

    //execute the method
    try {
        // Do it
        debug("(get)get=" + url);
        httpClient.executeMethod(method);

        // Process result
        result = method.getResponseBodyAsString();
        log("(get)" + method.getStatusLine().toString() + " size=" + result.length());

    } catch (HttpException he) {
        // Bad but not fatal
        error("(get)Error on connect to url " + url + ".  Error=" + he.getMessage());
    } catch (IOException ioe) {
        // Fatal
        throw buildException("(get)Unable to connect.  Session is invalid.  message=" + ioe.getMessage(),
                CallException.CODE_MODULE_FAULT, ioe);
    } finally {
        try {
            method.releaseConnection();
            method.recycle();
        } catch (Exception e) {
            // Already FUBAR
        }
    }
    return result;
}

From source file:com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.java

/**
 * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL)
 *///w ww  . ja  va 2  s  .  c  o m
public SyndFeed retrieveFeed(URL feedUrl)
        throws IllegalArgumentException, IOException, FeedException, FetcherException {
    if (feedUrl == null) {
        throw new IllegalArgumentException("null is not a valid URL");
    }
    // TODO Fix this
    //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    HttpClient client = new HttpClient(httpClientParams);

    if (getCredentialSupplier() != null) {
        client.getState().setAuthenticationPreemptive(true);
        // TODO what should realm be here?
        Credentials credentials = getCredentialSupplier().getCredentials(null, feedUrl.getHost());
        if (credentials != null) {
            client.getState().setCredentials(null, feedUrl.getHost(), credentials);
        }
    }

    System.setProperty("httpclient.useragent", getUserAgent());
    String urlStr = feedUrl.toString();

    HttpMethod method = new GetMethod(urlStr);
    method.addRequestHeader("Accept-Encoding", "gzip");
    method.addRequestHeader("User-Agent", getUserAgent());
    method.setFollowRedirects(true);

    if (httpClientMethodCallback != null) {
        synchronized (httpClientMethodCallback) {
            httpClientMethodCallback.afterHttpClientMethodCreate(method);
        }
    }

    FeedFetcherCache cache = getFeedInfoCache();
    if (cache != null) {
        // retrieve feed

        try {
            if (isUsingDeltaEncoding()) {
                method.setRequestHeader("A-IM", "feed");
            }

            // get the feed info from the cache
            // Note that syndFeedInfo will be null if it is not in the cache
            SyndFeedInfo syndFeedInfo = cache.getFeedInfo(feedUrl);
            if (syndFeedInfo != null) {
                method.setRequestHeader("If-None-Match", syndFeedInfo.getETag());

                if (syndFeedInfo.getLastModified() instanceof String) {
                    method.setRequestHeader("If-Modified-Since", (String) syndFeedInfo.getLastModified());
                }
            }

            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            SyndFeed feed = getFeed(syndFeedInfo, urlStr, method, statusCode);

            syndFeedInfo = buildSyndFeedInfo(feedUrl, urlStr, method, feed, statusCode);

            cache.setFeedInfo(new URL(urlStr), syndFeedInfo);

            // the feed may have been modified to pick up cached values
            // (eg - for delta encoding)
            feed = syndFeedInfo.getSyndFeed();

            return feed;
        } finally {
            method.releaseConnection();
            method.recycle();
        }

    } else {
        // cache is not in use          
        try {
            int statusCode = client.executeMethod(method);
            fireEvent(FetcherEvent.EVENT_TYPE_FEED_POLLED, urlStr);
            handleErrorCodes(statusCode);

            return getFeed(null, urlStr, method, statusCode);
        } finally {
            method.releaseConnection();
            method.recycle();
        }
    }
}