Example usage for org.apache.commons.httpclient HostConfiguration setHost

List of usage examples for org.apache.commons.httpclient HostConfiguration setHost

Introduction

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

Prototype

public void setHost(String paramString1, String paramString2, int paramInt, Protocol paramProtocol)

Source Link

Usage

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;/*from  ww w. jav a2  s .  co  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:fr.cls.atoll.motu.library.misc.cas.HttpClientTutorial.java

public static void test() {

    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;/*from   w w  w . ja  v  a2 s .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:org.mule.transport.http.MuleHostConfigurationTestCase.java

@SuppressWarnings("deprecation")
@Test/*from  w  w  w.  j av a2  s. com*/
public void testSetHostViaHostAndVirtualHostAndPortAndProtocol() {
    HostConfiguration hostConfig = createHostConfiguration();

    Protocol protocol = Protocol.getProtocol("http");
    hostConfig.setHost("www.mulesoft.org", "www.mulesoft.com", 8080, protocol);

    assertMockSocketFactory(hostConfig);
    assertEquals("www.mulesoft.org", hostConfig.getHost());
    assertEquals(8080, hostConfig.getPort());
    assertEquals("www.mulesoft.com", hostConfig.getVirtualHost());
}

From source file:org.mule.transport.http.MuleHostConfigurationTestCase.java

@SuppressWarnings("deprecation")
@Test//from w ww. j  a  va 2s  .  co  m
public void testSetHostViaHostAndVirtualHostAndPortAndProtocolWithDifferentProtocol() throws Exception {
    new DifferentProtocolTemplate() {
        protected void doTest() throws Exception {
            HostConfiguration hostConfig = createHostConfiguration();

            Protocol protocol = Protocol.getProtocol("httpx");
            hostConfig.setHost("www.mulesoft.org", "www.mulesoft.com", 8080, protocol);

            assertDefaultSocketFactory(hostConfig);
            assertEquals("www.mulesoft.org", hostConfig.getHost());
            assertEquals(8080, hostConfig.getPort());
            assertEquals("www.mulesoft.com", hostConfig.getVirtualHost());
        }
    }.test();
}