Example usage for org.apache.commons.httpclient HttpClient HttpClient

List of usage examples for org.apache.commons.httpclient HttpClient HttpClient

Introduction

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

Prototype

public HttpClient() 

Source Link

Usage

From source file:com.ebay.recommendations.RecommendationsSummary.java

/**
 * @param args/*w  ww . j a  va 2  s .c  o m*/
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    HttpClient client = new HttpClient();
    String fullUri = "https://svcs.ebay.com/services/selling/listingrecommendation/v1/item/recommendationsSummary";
    HttpMethod method = new GetMethod(fullUri);
    // REPLACE YOUR TOKEN IN THE <YOUR_TOKEN_HERE> PLACE HOLDER
    method.addRequestHeader("Authorization", "TOKEN <YOUR_TOKEN_HERE>");
    method.addRequestHeader("X-EBAY-GLOBAL-ID", "EBAY-US");

    method.addRequestHeader("Accept", "application/xml");

    try {
        client.executeMethod(method);
        String response = method.getResponseBodyAsString();
        System.out.println(response);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:BasicAuthenticationForJSPPage.java

public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    HostConfiguration host = client.getHostConfiguration();
    host.setHost(new URI("http://localhost:8080", true));

    Credentials credentials = new UsernamePasswordCredentials("tomcat", "tomcat");
    AuthScope authScope = new AuthScope(host.getHost(), host.getPort());
    HttpState state = client.getState();
    state.setCredentials(authScope, credentials);

    GetMethod method = new GetMethod("/commons/chapter01/protected.jsp");
    try {//www .  j  a  v a  2  s  .  c o  m
        client.executeMethod(host, method);
        System.err.println(method.getStatusLine());
        System.err.println(method.getResponseBodyAsString());
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:GetMethodExample.java

public static void main(String args[]) {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");
    client.getParams().setParameter("http.connection.timeout", new Integer(5000));

    GetMethod method = new GetMethod();
    FileOutputStream fos = null;/*w  w w  .  ja  v a 2s .  c o m*/

    try {

        method.setURI(new URI("http://www.google.com", true));
        int returnCode = client.executeMethod(method);

        if (returnCode != HttpStatus.SC_OK) {
            System.err.println("Unable to fetch default page, status code: " + returnCode);
        }

        System.err.println(method.getResponseBodyAsString());

        method.setURI(new URI("http://www.google.com/images/logo.gif", true));
        returnCode = client.executeMethod(method);

        if (returnCode != HttpStatus.SC_OK) {
            System.err.println("Unable to fetch image, status code: " + returnCode);
        }

        byte[] imageData = method.getResponseBody();
        fos = new FileOutputStream(new File("google.gif"));
        fos.write(imageData);

        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setHost("www.yahoo.com", null, 80, Protocol.getProtocol("http"));

        method.setURI(new URI("/", true));

        client.executeMethod(hostConfig, method);

        System.err.println(method.getResponseBodyAsString());

    } catch (HttpException he) {
        System.err.println(he);
    } catch (IOException ie) {
        System.err.println(ie);
    } finally {
        method.releaseConnection();
        if (fos != null)
            try {
                fos.close();
            } catch (Exception fe) {
            }
    }

}

From source file:com.blueferdi.diamondbox.jetty.connector.HttpClientDemo.java

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

    client.getHttpConnectionManager().getParams().setSoTimeout(1);

    int i = 1000;

    while (i > 0) {
        try {//from w w w  .  j a  v  a2s .c  o  m
            GetMethod method = new GetMethod("http://localhost:8080/testblocking/BlockingServlet");
            method.setRequestHeader("Connection", "close");
            client.executeMethod(method);
        } catch (Exception ex) {
            i--;
            System.out.println(i);
        }
    }
}

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 ww  w.  j  a  va  2 s . c o m
    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: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 {//w w w . j a  va2  s .c om
        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: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  ww. j  a  v a 2  s .  co m
    String response = method.getResponseBodyAsString();
    System.out.println(response);
    method.releaseConnection();
    method.recycle();
}

From source file:com.manning.blogapps.chapter10.examples.AuthDelete.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authdelete <username> <password> <url>");
        System.exit(-1);/* w  w w.j a v  a  2 s . c om*/
    }
    String credentials = args[0] + ":" + args[1];
    String url = args[2];

    HttpClient httpClient = new HttpClient();
    DeleteMethod method = new DeleteMethod(url);
    method.setRequestHeader("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    httpClient.executeMethod(method);
    System.out.println("Server returned status code: ");
}

From source file:com.manning.blogapps.chapter10.examples.AuthGet.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authget <username> <password> <url>");
        System.exit(-1);//from   w ww .  j ava2 s .com
    }
    String credentials = args[0] + ":" + args[1];
    String url = args[2];

    HttpClient httpClient = new HttpClient();
    GetMethod method = new GetMethod(url);
    method.setRequestHeader("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    httpClient.executeMethod(method);
    System.out.println(method.getResponseBodyAsString());
}

From source file:com.discursive.jccook.slide.ModifyExample.java

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

    String url = "http://www.discursive.com/jccook/dav/test.html";
    Credentials credentials = new UsernamePasswordCredentials("davuser", "davpass");

    // List resources in top directory
    WebdavResource resource = new WebdavResource(url, credentials);

    // Lock the Resource for 100 seconds
    boolean locked = resource.lockMethod("tobrien", 100);

    if (locked) {
        try {//from  ww  w  . j av  a  2s. co m
            // Read content as a String
            String resourceData = resource.getMethodDataAsString();
            printResource(resource, resourceData);

            // Modify a resource
            System.out.println("*** Modifying Resource");
            resourceData = resourceData.replaceAll("test", "modified test");
            resource.putMethod(resourceData);
        } finally {
            // Unlock the resource
            resource.unlockMethod("tobrien");
        }
    }

    // Close the resource   
    resource.close();
}