Example usage for org.apache.commons.httpclient.methods PostMethod recycle

List of usage examples for org.apache.commons.httpclient.methods PostMethod recycle

Introduction

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

Prototype

public void recycle() 

Source Link

Usage

From source file:com.sapienter.jbilling.client.api.APITest.java

License:asdf

private String[] makeCall(NameValuePair[] data, String separator) throws HttpException, IOException {

    Credentials creds = null;/*from w ww.ja va2  s.  c  o  m*/
    //            creds = new UsernamePasswordCredentials(args[1], args[2]);

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

    //set the default credentials
    if (creds != null) {
        client.getState().setCredentials(null, null, creds);
    }

    PostMethod post = new PostMethod("http://localhost/betty/gateway");

    post.setRequestBody(data);

    //execute the method
    String responseBody = null;
    client.executeMethod(post);
    responseBody = post.getResponseBodyAsString();

    System.out.println("Got response:" + responseBody);
    //write out the response body
    //clean up the connection resources
    post.releaseConnection();
    post.recycle();

    return responseBody.split(separator, -1);
}

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

/**
 * Post method. It will set the target address for the client, as well as
 * clearing any state./* ww w .  j  av a  2  s  .  c om*/
 * 
 * @param url
 *            the Url path, not to include protocol, address, and port (ie.
 *            "/goats/index.html").
 * @param nv
 *            set of name/value pairs for the post. it can be empty.
 * @return the data from the page as a String
 * @throws CallException
 */
private String post(String url, Hashtable nv) throws CallException {

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

    String result = null;
    String name;
    Object value;

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

    //build the rest of the method
    try {
        // Construct the headers
        Enumeration eNV = nv.keys();
        while (eNV.hasMoreElements()) {
            name = (String) eNV.nextElement();
            value = nv.get(name);
            if (value instanceof String) {
                // Only take it if it is a string
                method.addParameter(name, (String) value);
                debug("ADD POST - name=" + name + " value=" + (String) value);
            }
        }
        //DEBUG
        debug("DUMP POST-------------------------------");
        debug(method.toString());
        debug("DUMP POST-------------------------------");

        // Do it
        debug("(post)post=" + url);
        httpClient.executeMethod(method);

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

    } catch (HttpException he) {
        // Bad but not fatal
        error("(post)Error on connect to url " + url + ".  Error=" + he.getMessage());
    } catch (IOException ioe) {
        // Fatal
        throw buildException("(post)Unable to connect.  Session is invalid.", CallException.CODE_MODULE_FAULT,
                ioe);

    } catch (Exception ex) {
        // Fatal
        throw buildException("(post)Serious general error.", CallException.CODE_MODULE_FAULT, ex);
    } finally {
        try {
            method.releaseConnection();
            method.recycle();
        } catch (Exception e) {
            // Already FUBAR
        }
    }
    return result;
}