Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

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

Prototype

<T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
        throws IOException, ClientProtocolException;

Source Link

Document

Executes HTTP request using the default context and processes the response using the given response handler.

Usage

From source file:org.magnum.mobilecloud.video.AutoGrading.java

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

    // Ensure that this application is run within the correct working directory
    File f = new File("./src/main/java/org/magnum/mobilecloud/video/Application.java");
    if (!f.exists()) {
        System.out.println(WordUtils.wrap("You must run the AutoGrading application from the root of the "
                + "project directory containing src/main/java. If you right-click->Run As->Java Application "
                + "in Eclipse, it will automatically use the correct classpath and working directory "
                + "(assuming that you have Gradle and the project setup correctly).", 80));
        System.exit(1);/* ww  w . j a  va2 s  .  c  om*/
    }

    // Ensure that the server is running and accessible on port 8443
    try {
        HttpClient client = UnsafeHttpsClient.createUnsafeClient();
        HttpResponse response = client.execute(new HttpHost("127.0.0.1", 8443),
                new BasicHttpRequest("GET", "/"));

        response.getStatusLine();
    } catch (NoHttpResponseException e) {
        // The server may have returned some JSON object
    } catch (Exception e) {
        System.out.println(WordUtils.wrap(
                "Unable to connect to your server on https://localhost:8443. Are you sure the server is running? "
                        + "In order to run the autograder, you must first launch your application "
                        + "by right-clicking on the Application class in Eclipse, and"
                        + "choosing Run As->Java Application. If you have already done this, make sure that"
                        + " you can access your server by opening the https://localhost:8443 url in a browser. "
                        + "If you can't access the server in a browser, it probably indicates you have a firewall "
                        + "or some other issue that is blocking access to port 8080 on localhost.",
                80));
        System.exit(1);
    }

    HandinUtil.generateHandinPackage("Asgn2", new File("./"), InternalAutoGradingTest.class);
}

From source file:localhost.potlatchserver.AutoGrading.java

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

    // Ensure that this application is run within the correct working directory
    File f = new File("./src/main/java/localhost/potlatchserver/Application.java");
    if (!f.exists()) {
        System.out.println(WordUtils.wrap("You must run the AutoGrading application from the root of the "
                + "project directory containing src/main/java. If you right-click->Run As->Java Application "
                + "in Eclipse, it will automatically use the correct classpath and working directory "
                + "(assuming that you have Gradle and the project setup correctly).", 80));
        System.exit(1);/* w  w w .ja  v  a2s .  co m*/
    }

    // Ensure that the server is running and accessible on port 8443
    try {
        HttpClient client = UnsafeHttpsClient.createUnsafeClient();
        HttpResponse response = client.execute(new HttpHost("127.0.0.1", 8443),
                new BasicHttpRequest("GET", "/"));

        response.getStatusLine();
    } catch (NoHttpResponseException e) {
        // The server may have returned some JSON object
    } catch (Exception e) {
        System.out.println(WordUtils.wrap(
                "Unable to connect to your server on https://localhost:8443. Are you sure the server is running? "
                        + "In order to run the autograder, you must first launch your application "
                        + "by right-clicking on the Application class in Eclipse, and"
                        + "choosing Run As->Java Application. If you have already done this, make sure that"
                        + " you can access your server by opening the https://localhost:8443 url in a browser. "
                        + "If you can't access the server in a browser, it probably indicates you have a firewall "
                        + "or some other issue that is blocking access to port 8080 on localhost.",
                80));
        System.exit(1);
    }

    HandinUtil.generateHandinPackage("Asgn2", new File("./"), InternalAutoGradingTest.class);
}

From source file:yangqi.hc.HttpContextTest.java

/**
 * @param args//from   w w w.  j av  a2 s . c o  m
 * @throws IOException
 * @throws ClientProtocolException
 */
public static void main(String[] args) throws ClientProtocolException, IOException {
    // TODO Auto-generated method stub
    HttpClient httpclient = new DefaultHttpClient();

    HttpContext context = new BasicHttpContext();

    // Prepare a request object
    HttpGet httpget = new HttpGet("http://www.apache.org/");

    // Execute the request
    HttpResponse response = httpclient.execute(httpget, context);

    showAttr(ExecutionContext.HTTP_CONNECTION, context);
    showAttr(ExecutionContext.HTTP_PROXY_HOST, context);
    showAttr(ExecutionContext.HTTP_REQ_SENT, context);
    showAttr(ExecutionContext.HTTP_RESPONSE, context);
    showAttr(ExecutionContext.HTTP_REQUEST, context);
    showAttr(ExecutionContext.HTTP_TARGET_HOST, context);

}

From source file:com.hilatest.httpclient.apacheexample.ClientWithResponseHandler.java

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

    HttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("http://www.google.com/");

    System.out.println("executing request " + httpget.getURI());

    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println(responseBody);

    System.out.println("----------------------------------------");

    // 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.httpclient.lesson02.java

public static void main(String[] args) {

    HttpClient httpclient = new DefaultHttpClient();

    try {/*from  ww w  . j  a  va 2 s. c  o  m*/
        HttpGet get = new HttpGet("http://www.google.com");
        System.out.println("execute request: " + get.getURI());
        //create a response hendler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(get, responseHandler);
        System.out.println("====================================");
        System.out.println(responseBody);
        System.out.println("====================================");

    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        //???
        httpclient.getConnectionManager().shutdown();
    }

}

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

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

    HttpClient httpclient = new DefaultHttpClient();
    try {// w  w w.  j  a v  a  2s.c o  m
        HttpGet httpget = new HttpGet("http://www.google.com/");

        System.out.println("executing request " + httpget.getURI());

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(responseBody);
        System.out.println("----------------------------------------");

    } 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.http.test.ClientWithResponseHandler.java

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

    HttpClient httpclient = new DefaultHttpClient();
    try {// w w  w .j av  a2 s.com
        HttpGet httpget = new HttpGet("http://191.98.138.6/");

        System.out.println("executing request " + httpget.getURI());

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        org.apache.http.Header h[] = httpget.getAllHeaders();
        int i = 0;
        System.out.println("----------------------------------------");
        for (i = 0; i < h.length; i++) {
            System.out.println(h[i].toString());
        }
        System.out.println(responseBody);
        System.out.println("----------------------------------------");

    } 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.mtea.macrotea_httpclient_study.ClientWithResponseHandler.java

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

    HttpClient httpclient = new DefaultHttpClient();
    try {/*from  w ww .j a  v a2s  . c o m*/
        HttpGet httpget = new HttpGet("http://www.google.com/");

        System.out.println("executing request " + httpget.getURI());

        // ResponseHandler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        //???BasicResponseHandler
        String responseBody = httpclient.execute(httpget, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(responseBody);
        System.out.println("----------------------------------------");

    } finally {
        //??httpclient???
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.liferay.mobile.sdk.BuilderAntTask.java

public static void main(String[] args) {
    Map<String, String> arguments = parseArguments(args);

    String url = arguments.get("url");
    String context = arguments.get("context");
    String filter = arguments.get("filter");

    StringBuilder sb = new StringBuilder();

    sb.append(url);//from   w  w  w .  ja v  a 2 s.co m

    if (Validator.isNotNull(context)) {
        sb.append("/");
        sb.append(context);
    }

    sb.append("/api/jsonws?discover");

    if (Validator.isNull(filter)) {
        sb.append("=/*");
    } else {
        sb.append("=/");
        sb.append(filter);
        sb.append("/*");
    }

    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(sb.toString());

    DiscoveryResponseHandler handler = new DiscoveryResponseHandler();

    try {
        String builderType = arguments.get("builder");

        Builder builder = null;

        if (builderType.equals("android")) {
            builder = new AndroidBuilder();
        }

        Discovery discovery = client.execute(get, handler);

        PortalVersion version = HttpUtil.getPortalVersion(url);

        if (Validator.isNull(filter)) {
            builder.buildAll(version, discovery);
        } else {
            builder.build(filter, version, discovery);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.music.tools.SongDBDownloader.java

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

    //        HttpHost proxy = new HttpHost("localhost", 8888);
    //        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    HttpContext ctx = new BasicHttpContext();

    HttpUriRequest req = new HttpGet(
            "http://www.hooktheory.com/analysis/view/the-beatles/i-want-to-hold-your-hand");
    client.execute(req, ctx);
    req.abort();// ww  w. ja v  a  2  s .c  o m

    List<String> urls = getSongUrls(
            "http://www.hooktheory.com/analysis/browseSearch?sQuery=&sOrderBy=views&nResultsPerPage=525&nPage=1",
            client, ctx);
    List<List<? extends NameValuePair>> paramsList = new ArrayList<>(urls.size());
    for (String songUrl : urls) {
        paramsList.addAll(getSongParams(songUrl, client, ctx));
    }
    int i = 0;
    for (List<? extends NameValuePair> params : paramsList) {

        HttpPost request = new HttpPost("http://www.hooktheory.com/songs/getXML");

        request.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
        request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setHeader("Accept-Encoding", "gzip, deflate");
        request.setHeader("Accept-Language", "en,en-us;q=0.7,bg;q=0.3");
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");
        request.setHeader("Origin", "http://www.hooktheory.com");
        request.setHeader("Referer",
                URLEncoder.encode("http://www.hooktheory.com/swf/DNALive Version 1.0.131.swf", "utf-8"));

        HttpEntity entity = new UrlEncodedFormEntity(params);
        request.setEntity(entity);

        try {
            HttpResponse response = client.execute(request, ctx);
            if (response.getStatusLine().getStatusCode() == 200) {
                InputStream is = response.getEntity().getContent();
                String xml = CharStreams.toString(new InputStreamReader(is));
                is.close();
                Files.write(xml, new File("c:/tmp/musicdb/" + i + ".xml"), Charset.forName("utf-8"));
            } else {
                System.out.println(response.getStatusLine());
                System.out.println(params);
            }
            i++;
            request.abort();
        } catch (Exception ex) {
            System.out.println(params);
            ex.printStackTrace();
        }
    }
}