Example usage for org.apache.http.impl.client DefaultHttpClient execute

List of usage examples for org.apache.http.impl.client DefaultHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpHost target, final HttpRequest request,
        final HttpContext context) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:httpclient.client.ClientPreemptiveBasicAuthentication.java

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

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 80),
            new UsernamePasswordCredentials("username", "password"));

    BasicHttpContext localcontext = new BasicHttpContext();

    // Generate BASIC scheme object and stick it to the local 
    // execution context
    BasicScheme basicAuth = new BasicScheme();
    localcontext.setAttribute("preemptive-auth", basicAuth);

    // Add as the first request interceptor
    httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);

    HttpHost targetHost = new HttpHost("localhost", 80, "http");

    HttpGet httpget = new HttpGet("/");

    System.out.println("executing request: " + httpget.getRequestLine());
    System.out.println("to target: " + targetHost);

    for (int i = 0; i < 3; i++) {
        HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            entity.consumeContent();/*ww  w  .  java 2 s.c  o  m*/
        }
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

From source file:com.dlmu.heipacker.crawler.client.ClientPreemptiveBasicAuthentication.java

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

    HttpHost targetHost = new HttpHost("localhost", 80, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*  ww  w  .jav a2 s .c o m*/
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("/");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.slieer.http.auth.ClientPreemptiveBasicAuthentication.java

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

    HttpHost targetHost = new HttpHost("localhost", 8080, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*from  www.  j a v  a 2 s.c o  m*/
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("newuser", "tomcat"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("/simpleweb/protected");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.hsbc.frc.SevenHero.ClientPreemptiveBasicAuthentication.java

public static void main(String[] args) throws Exception {
    HttpHost proxy = new HttpHost("133.13.162.149", 8080, "http");
    HttpHost targetHost = new HttpHost("pt.3g.qq.com");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from  w  w w .j  a va 2 s  .  co m
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local
        // auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("/");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.dlmu.heipacker.crawler.client.ClientPreemptiveDigestAuthentication.java

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

    HttpHost targetHost = new HttpHost("localhost", 80, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from w  w  w  . j  a v  a2s. c om
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();
        // Suppose we already know the realm name
        digestAuth.overrideParamter("realm", "some realm");
        // Suppose we already know the expected nonce value
        digestAuth.overrideParamter("nonce", "whatever");
        authCache.put(targetHost, digestAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("/");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        }

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:httpclient.client.ClientPreemptiveDigestAuthentication.java

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

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 80),
            new UsernamePasswordCredentials("username", "password"));

    BasicHttpContext localcontext = new BasicHttpContext();
    // Generate DIGEST scheme object, initialize it and stick it to 
    // the local execution context
    DigestScheme digestAuth = new DigestScheme();
    // Suppose we already know the realm name
    digestAuth.overrideParamter("realm", "some realm");
    // Suppose we already know the expected nonce value 
    digestAuth.overrideParamter("nonce", "whatever");
    localcontext.setAttribute("preemptive-auth", digestAuth);

    // Add as the first request interceptor
    httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);
    // Add as the last response interceptor
    httpclient.addResponseInterceptor(new PersistentDigest());

    HttpHost targetHost = new HttpHost("localhost", 80, "http");

    HttpGet httpget = new HttpGet("/");

    System.out.println("executing request: " + httpget.getRequestLine());
    System.out.println("to target: " + targetHost);

    for (int i = 0; i < 3; i++) {
        HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            entity.consumeContent();//from   w  w  w .  j av a 2s. c o m
        }
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

From source file:be.cytomine.client.HttpClient.java

public static BufferedImage readBufferedImageFromPOST(String url, String post) throws IOException {
    log.debug("readBufferedImageFromURL:" + url);
    URL URL = new URL(url);
    HttpHost targetHost = new HttpHost(URL.getHost(), URL.getPort());
    log.debug("targetHost:" + targetHost);
    DefaultHttpClient client = new DefaultHttpClient();

    log.debug("client:" + client);
    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    log.debug("localcontext:" + localcontext);

    BufferedImage img = null;/*from   w w  w. j a va  2  s.c o  m*/
    HttpPost httpPost = new HttpPost(URL.toString());
    httpPost.setEntity(new StringEntity(post, "UTF-8"));
    HttpResponse response = client.execute(targetHost, httpPost, localcontext);

    int code = response.getStatusLine().getStatusCode();
    log.info("url=" + url + " is " + code + "(OK=" + HttpURLConnection.HTTP_OK + ",MOVED="
            + HttpURLConnection.HTTP_MOVED_TEMP + ")");

    boolean isOK = (code == HttpURLConnection.HTTP_OK);
    boolean isFound = (code == HttpURLConnection.HTTP_MOVED_TEMP);
    boolean isErrorServer = (code == HttpURLConnection.HTTP_INTERNAL_ERROR);

    if (!isOK && !isFound & !isErrorServer)
        throw new IOException(url + " cannot be read: " + code);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        img = ImageIO.read(entity.getContent());
    }
    return img;
}

From source file:be.cytomine.client.HttpClient.java

public static BufferedImage readBufferedImageFromRETRIEVAL(String url, String publicKey, String privateKey,
        String host) throws IOException {
    log.debug("readBufferedImageFromURL:" + url);
    URL URL = new URL(url);
    HttpHost targetHost = new HttpHost(URL.getHost(), URL.getPort());
    log.debug("targetHost:" + targetHost);
    DefaultHttpClient client = new DefaultHttpClient();
    log.debug("client:" + client);
    // Add AuthCache to the execution context
    BasicHttpContext localcontext = new BasicHttpContext();
    log.debug("localcontext:" + localcontext);
    Header[] headers = authorizeFromRETRIEVAL("GET", URL.toString(), "", "", publicKey, privateKey, host);
    log.debug("headers:" + headers.length);

    BufferedImage img = null;/*from w  w  w. j av a2 s . c om*/
    HttpGet httpGet = new HttpGet(URL.toString());
    httpGet.setHeaders(headers);
    HttpResponse response = client.execute(targetHost, httpGet, localcontext);
    int code = response.getStatusLine().getStatusCode();
    log.info("url=" + url + " is " + code + "(OK=" + HttpURLConnection.HTTP_OK + ",MOVED="
            + HttpURLConnection.HTTP_MOVED_TEMP + ")");

    boolean isOK = (code == HttpURLConnection.HTTP_OK);
    boolean isFound = (code == HttpURLConnection.HTTP_MOVED_TEMP);
    boolean isErrorServer = (code == HttpURLConnection.HTTP_INTERNAL_ERROR);

    if (!isOK && !isFound & !isErrorServer) {
        throw new IOException(url + " cannot be read: " + code);
    }
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        log.debug("img=" + entity.getContent());
        img = ImageIO.read(entity.getContent());
    }
    return img;

}