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

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

Introduction

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

Prototype

public abstract void releaseConnection();

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 .  ja v  a2s.  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);/*from  w  w  w. jav a2s.c  om*/
    String response = method.getResponseBodyAsString();
    System.out.println(response);
    method.releaseConnection();
    method.recycle();
}

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

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    // Set credentials on the client
    Credentials credentials = new NTCredentials("testuser", "crazypass", "tobrien.discursive.com", "DOMAIN");
    client.getState().setCredentials(null, null, credentials);

    String url = "http://webmail.domain.biz/exchange/";
    HttpMethod method = new GetMethod(url);

    client.executeMethod(method);//  w ww. j a  va  2 s  .c  o m
    String response = method.getResponseBodyAsString();

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

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);/*  w  w w  . j a  va2s .co  m*/
    String response = method.getResponseBodyAsString();

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

From source file:com.discursive.jccook.httpclient.BasicAuthExample.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();
    HttpState state = client.getState();
    HttpClientParams params = client.getParams();

    // Set credentials on the client
    Credentials credentials = new UsernamePasswordCredentials("testuser", "crazypass");
    state.setCredentials(null, null, credentials);
    params.setAuthenticationPreemptive(true);

    String url = "http://www.discursive.com/jccook/auth/";
    HttpMethod method = new GetMethod(url);

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

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

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

public static void main(String[] args) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi";

    // Set the Query String with setQueryString()
    HttpMethod method = new GetMethod(url);
    method.setQueryString(URIUtil.encodeQuery("test1=O'Reilly&blah=Whoop"));
    System.out.println("With Query String: " + method.getURI());
    client.executeMethod(method);/*from  w ww.j  av  a 2s  .  co m*/
    System.out.println("Response:\n " + method.getResponseBodyAsString());
    method.releaseConnection();

    // Set query string with name value pair objects
    method = new GetMethod(url);
    NameValuePair pair = new NameValuePair("test2", URIUtil.encodeQuery("One & Two"));
    NameValuePair pair2 = new NameValuePair("param2", URIUtil.encodeQuery("TSCM"));
    NameValuePair[] pairs = new NameValuePair[] { pair, pair2 };
    method.setQueryString(pairs);
    System.out.println("With NameValuePairs: " + method.getURI());
    client.executeMethod(method);
    System.out.println("Response:\n " + method.getResponseBodyAsString());
    method.releaseConnection();
}

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 ww  .java2s .  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:Correct.java

public static void main(String[] args) {

    String URLL = "";
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod(URLL);
    method.setDoAuthentication(true);//from  w  w w .j a v a 2s . com
    HostConfiguration hostConfig = client.getHostConfiguration();
    hostConfig.setHost("172.29.38.8");
    hostConfig.setProxy("172.29.90.4", 8);
    //   NTCredentials proxyCredentials = new NTCredentials("", "", "", "");
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
    ////        try {
    ////            URL url = new URL("");
    ////            HttpURLConnection urls = (HttpURLConnection) url.openConnection();
    ////            
    ////           
    ////        } catch (MalformedURLException ex) {
    ////            Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex);
    ////        } catch (IOException ex) {
    ////            Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex);
    ////        }
    try {
        // send the transaction
        client.executeMethod(hostConfig, method);
        StatusLine status = method.getStatusLine();

        if (status != null && method.getStatusCode() == 200) {

            System.out.println(method.getResponseBodyAsString() + "\n Status code : " + status);

        } else {

            System.err.println(method.getStatusLine() + "\n: Posting Failed !");
        }

    } catch (IOException ioe) {

        ioe.printStackTrace();

    }
    method.releaseConnection();

}

From source file:TrivialApp.java

public static void main(String[] args) {
    if ((args.length != 1) && (args.length != 3)) {
        printUsage();/*from  w  ww. j  a  v a2s  .c om*/
        System.exit(-1);
    }

    Credentials creds = null;
    if (args.length >= 3) {
        creds = new UsernamePasswordCredentials(args[1], args[2]);
    }

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

    //establish a connection within 5 seconds
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

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

    String url = args[0];
    HttpMethod method = null;

    //create a method object
    method = new GetMethod(url);
    method.setFollowRedirects(true);
    //} 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();

    System.exit(0);
}

From source file:ExampleP2PHttpClient.java

public static void main(String[] args) {
    // initialize JXTA
    try {/*from  w w w  .j ava  2 s  . com*/
        // 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);
}