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:HttpClientTest.java

public static void main(String args[]) throws Exception {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod("http://www.google.com");
    int returnCode = client.executeMethod(method);
    System.err.println(method.getResponseBodyAsString());
    method.releaseConnection();//from w ww.ja  v  a  2 s. com
}

From source file:HttpClientPreferences.java

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

    System.err.println(/*from   ww w. j  a  va2 s. com*/
            "The User Agent before changing it is: " + client.getParams().getParameter("http.useragent"));

    client.getParams().setParameter("http.useragent", "Browser at Client level");

    System.err.println("Client's User Agent is: " + client.getParams().getParameter("http.useragent"));

    GetMethod method = new GetMethod("http://www.google.com");
    method.getParams().setParameter("http.useragent", "Browser at Method level");
    try {
        client.executeMethod(method);
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
    System.err.println("Method's User Agent is: " + method.getParams().getParameter("http.useragent"));

}

From source file:OptionsMethodExample.java

public static void main(String args[]) {

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

    OptionsMethod method = new OptionsMethod("http://www.google.com");

    try {//from  ww  w . ja v  a2 s  . co  m
        int returnCode = client.executeMethod(method);

        Enumeration list = method.getAllowedMethods();

        while (list.hasMoreElements()) {
            System.err.println(list.nextElement());
        }

    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }

}

From source file:GetCookiePrintAndSetValue.java

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

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

    GetMethod method = new GetMethod("http://localhost:8080/");
    try {//from  ww  w  .ja v  a  2s. co  m
        client.executeMethod(method);
        Cookie[] cookies = client.getState().getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            System.err.println("Cookie: " + cookie.getName() + ", Value: " + cookie.getValue()
                    + ", IsPersistent?: " + cookie.isPersistent() + ", Expiry Date: " + cookie.getExpiryDate()
                    + ", Comment: " + cookie.getComment());

            cookie.setValue("My own value");
        }
        client.executeMethod(method);
    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:HttpClientParameter.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("www.google.com");

    GetMethod method = new GetMethod("http://www.yahoo.com");

    int returnCode = client.executeMethod(host, method);

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

    System.err/*from  w  w  w. j ava 2  s . c o  m*/
            .println("User-Agent: " + method.getHostConfiguration().getParams().getParameter("http.useragent"));

    System.err.println("User-Agent: " + method.getParams().getParameter("http.useragent"));

    method.releaseConnection();
}

From source file:BasicAuthenticationExecuteJSPMethod.java

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

    HttpClient client = new HttpClient();
    client.getParams().setParameter("parameterKey", "value");

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

    GetMethod method = new GetMethod("/commons/folder/protected.jsp");
    try {/*  ww  w.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:UsingHttpClientInsideThread.java

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

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

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

    // first Get a big file
    MethodThread bigDataThread = new MethodThread(client, host, "/big_movie.wmv");

    bigDataThread.start();/*from   ww w .j a  v a  2s  .  c  om*/

    // next try and get a small file
    MethodThread normalThread = new MethodThread(client, host, "/");

    normalThread.start();
}

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

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

    HttpClient client = new HttpClient();
    String fullUri = "https://svcs.ebay.com/services/selling/listingrecommendation/v1/item/300878655630/itemRecommendations/";
    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:com.ebay.recommendations.LatestVersion.java

/**
 * @param args/*from   www  . jav a  2s.co  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/latestVersion";
    HttpMethod method = new GetMethod(fullUri);
    // REPLACE YOUR TOKEN IN THE <YOUR_TOKEN_HERE> PLACE HOLDER
    method.addRequestHeader("Authorization", "TOKEN <YOUR_TOKEN_HERE>");
    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:com.ebay.recommendations.RecommendationItemIds.java

/**
 * @param args/*from  ww  w  . ja  v a 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/recommendationItemIds?recommendationType=Picture&pageNumber=1&entriesPerPage=5";
    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();
    }

}