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(URI paramURI) 

Source Link

Usage

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 {/*from   ww  w  . ja  va 2  s.  com*/
        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: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// w w w.  j a v  a2s.  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: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 {/*  w  w  w.j a va  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  w  w  w.  ja v a 2  s. c o m

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

    normalThread.start();
}

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  a2 s.  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:com.liferay.util.Http.java

public static byte[] URLtoByteArray(String location, Cookie[] cookies, boolean post) throws IOException {

    byte[] byteArray = null;

    HttpMethod method = null;//w  ww  .j a v  a 2  s.  c o m

    try {
        HttpClient client = new HttpClient(new SimpleHttpConnectionManager());

        if (location == null) {
            return byteArray;
        } else if (!location.startsWith(HTTP_WITH_SLASH) && !location.startsWith(HTTPS_WITH_SLASH)) {

            location = HTTP_WITH_SLASH + location;
        }

        HostConfiguration hostConfig = new HostConfiguration();

        hostConfig.setHost(new URI(location));

        if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) {
            hostConfig.setProxy(PROXY_HOST, PROXY_PORT);
        }

        client.setHostConfiguration(hostConfig);
        client.setConnectionTimeout(5000);
        client.setTimeout(5000);

        if (cookies != null && cookies.length > 0) {
            HttpState state = new HttpState();

            state.addCookies(cookies);
            state.setCookiePolicy(CookiePolicy.COMPATIBILITY);

            client.setState(state);
        }

        if (post) {
            method = new PostMethod(location);
        } else {
            method = new GetMethod(location);
        }

        method.setFollowRedirects(true);

        client.executeMethod(method);

        Header locationHeader = method.getResponseHeader("location");
        if (locationHeader != null) {
            return URLtoByteArray(locationHeader.getValue(), cookies, post);
        }

        InputStream is = method.getResponseBodyAsStream();

        if (is != null) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            byte[] bytes = new byte[512];

            for (int i = is.read(bytes, 0, 512); i != -1; i = is.read(bytes, 0, 512)) {

                buffer.write(bytes, 0, i);
            }

            byteArray = buffer.toByteArray();

            is.close();
            buffer.close();
        }

        return byteArray;
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception e) {
            Logger.error(Http.class, e.getMessage(), e);
        }
    }
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Set http client current host configuration.
 *
 * @param httpClient current Http client
 * @param url        target url/*from  w w  w.  j  a v a2  s  . c om*/
 * @throws DavMailException on error
 */
public static void setClientHost(HttpClient httpClient, String url) throws DavMailException {
    try {
        HostConfiguration hostConfig = httpClient.getHostConfiguration();
        URI httpURI = new URI(url, true);
        hostConfig.setHost(httpURI);
    } catch (URIException e) {
        throw new DavMailException("LOG_INVALID_URL", url);
    }
}

From source file:de.softwareforge.pgpsigner.util.HKPSender.java

public HKPSender(final String serverName) {
    HttpHost httpHost = new HttpHost(serverName, HKP_DEFAULT_PORT);
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(httpHost);
    this.httpClient = new HttpClient();
    this.httpClient.getParams().setParameter("http.useragent", PGPSigner.APPLICATION_VERSION);
    httpClient.setHostConfiguration(hostConfig);
}

From source file:edu.tsinghua.lumaqq.test.CustomHeadUploader.java

/**
 * /*from www.  ja va2s .  c o  m*/
 * 
 * @param filename
 */
public void upload(String filename, QQUser user) {
    HttpClient client = new HttpClient();
    HostConfiguration conf = new HostConfiguration();
    conf.setHost(QQ.QQ_SERVER_UPLOAD_CUSTOM_HEAD);
    client.setHostConfiguration(conf);
    PostMethod method = new PostMethod("/cgi-bin/cface/upload");
    method.addRequestHeader("Accept", "*/*");
    method.addRequestHeader("Pragma", "no-cache");

    StringPart uid = new StringPart("clientuin", String.valueOf(user.getQQ()));
    uid.setContentType(null);
    uid.setTransferEncoding(null);

    //StringPart clientkey = new StringPart("clientkey", "0D649E66B0C1DBBDB522CE9C846754EF6AFA10BBF1A48A532DF6369BBCEF6EE7");
    //StringPart clientkey = new StringPart("clientkey", "3285284145CC19EC0FFB3B25E4F6817FF3818B0E72F1C4E017D9238053BA2040");
    StringPart clientkey = new StringPart("clientkey",
            "2FEEBE858DAEDFE6352870E32E5297ABBFC8C87125F198A5232FA7ADA9EADE67");
    //StringPart clientkey = new StringPart("clientkey", "8FD643A7913C785AB612F126C6CD68A253F459B90EBCFD9375053C159418AF16");
    //      StringPart clientkey = new StringPart("clientkey", Util.convertByteToHexStringWithoutSpace(user.getClientKey()));
    clientkey.setContentType(null);
    clientkey.setTransferEncoding(null);

    //StringPart sign = new StringPart("sign", "2D139466226299A73F8BCDA7EE9AB157E8D9DA0BB38B6F4942A1658A00BD4C1FEE415838810E5AEF40B90E2AA384A875");
    //StringPart sign = new StringPart("sign", "50F479417088F26EFC75B9BCCF945F35346188BB9ADD3BDF82098C9881DA086E3B28D56726D6CB2331909B62459E1E62");
    //StringPart sign = new StringPart("sign", "31A69198C19A7C4BFB60DCE352FE2CC92C9D27E7C7FEADE1CBAAFD988906981ECC0DD1782CBAE88A2B716F84F9E285AA");
    StringPart sign = new StringPart("sign",
            "68FFB636DE63D164D4072D7581213C77EC7B425DDFEB155428768E1E409935AA688AC88910A74C5D2D94D5EF2A3D1764");
    sign.setContentType(null);
    sign.setTransferEncoding(null);

    FilePart file;
    try {
        file = new FilePart("customfacefile", filename, new File(filename));
    } catch (FileNotFoundException e) {
        return;
    }

    Part[] parts = new Part[] { uid, clientkey, sign, file };
    MultipartRequestEntity entity = new MultipartRequestEntity(parts, method.getParams());

    method.setRequestEntity(entity);
    try {
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            Header header = method.getResponseHeader("CFace-Msg");
            System.out.println(header.getValue());
            header = method.getResponseHeader("CFace-RetCode");
            System.out.println(header.getValue());
        }
    } catch (HttpException e) {
        return;
    } catch (IOException e) {
        return;
    } finally {
        method.releaseConnection();
    }
}

From source file:net.mojodna.searchable.solr.SolrIndexer.java

private HttpClient getHttpClient() throws URIException {
    final HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost(new URI("http", null, solrHost, solrPort, solrPath));
    httpClient.setHostConfiguration(hostConfig);
    return httpClient;
}