Example usage for org.apache.http.impl.client BasicResponseHandler BasicResponseHandler

List of usage examples for org.apache.http.impl.client BasicResponseHandler BasicResponseHandler

Introduction

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

Prototype

BasicResponseHandler

Source Link

Usage

From source file:org.httpclient.lesson02.java

public static void main(String[] args) {

    HttpClient httpclient = new DefaultHttpClient();

    try {/*  w  ww . j ava 2s.c om*/
        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.mtea.macrotea_httpclient_study.ClientWithResponseHandler.java

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

    HttpClient httpclient = new DefaultHttpClient();
    try {//from w  ww.j ava 2s. 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:edu.ucsd.ccdb.cil.xml2json.ElasticsearchClient.java

public static void main(String[] args) throws Exception {
    try {/*from  w w  w . j a  va  2s . c  o  m*/
        DefaultHttpClient httpClient = new DefaultHttpClient();

        //HttpPut put = new HttpPut("http://localhost:9200/customer/user/john@smith.com");  //-X PUT
        //put.setEntity(new FileEntity(new File("/Users/ncmir/NetBeansProjects/MyTest/web/customer.json"), "application/json"));  //@ - absolute path
        HttpPut put = new HttpPut("http://localhost:9200/ccdb/data/1"); //-X PUT
        put.setEntity(
                new FileEntity(new File("/Users/ncmir/Documents/CCDB/ccdbJson/1.json"), "application/json")); //@ - absolute path

        BasicResponseHandler responseHandler = new BasicResponseHandler();

        String o = httpClient.execute(put, responseHandler);
        System.out.println(o);
    } catch (Exception e) {
        //-f, fail silently
        e.printStackTrace();
    }
}

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

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

    HttpClient httpclient = new DefaultHttpClient();
    try {//ww  w.  j  a v a  2s . c om
        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.kookoo.outbound.OutboundTest.java

public static void main(String a[]) {
    try {/*from   w  w w.j a va2s  .c o  m*/
        String server_ip = "http://XXXX:8080"; /*change to your web server*/
        String phone_no1 = "09985XXXXX";/*change number to your numbers*/
        String phone_no2 = "09985XXXXX";/*change number to your numbers*/
        String api_key = "KKXXXXX";/*kookoo api key*/
        String kookoo_number = "91xxxxx";/*kookoo assigned number*/

        Date d = new Date();
        String trackId = "" + d.getTime();
        String url = "http://kookoo.in/outbound/outbound.php";
        URIBuilder uribuilder = new URIBuilder(url);
        uribuilder.addParameter("api_key", api_key);
        uribuilder.addParameter("phone_no", phone_no1);
        uribuilder.addParameter("caller_id", kookoo_number);
        /*assigned kookoo number*/
        uribuilder.addParameter("url",
                server_ip + "/kookoocall/outboundcall?number2=" + phone_no2 + "&trackId=" + trackId);
        uribuilder.addParameter("callback_url",
                server_ip + "/kookoocall/outbound_callstatus?number2=" + phone_no2 + "&trackId=" + trackId);

        URI uri = uribuilder.build();
        System.out.println("Final Outboud API url " + uri);
        HttpGet request = new HttpGet(uri);
        HttpClient client = HttpClientBuilder.create().build();
        HttpResponse response = client.execute(request);

        String responseString = new BasicResponseHandler().handleResponse(response);
        System.out.println(responseString);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

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

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

    HttpClient httpclient = new DefaultHttpClient();
    try {//from   w ww  . j  ava  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.cloudhopper.sxmp.PostMO.java

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

    String URL = "https://sms.twitter.com/receive/cloudhopper";
    String text = "HELP";
    String srcAddr = "+16504304922";

    String ticketId = System.currentTimeMillis() + "";
    String operatorId = "20";

    StringBuilder string0 = new StringBuilder(200).append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
            .append("<operation type=\"deliver\">\n")
            .append(" <account username=\"customer1\" password=\"password1\"/>\n").append(" <deliverRequest>\n")
            .append("  <operatorId>" + operatorId + "</operatorId>\n")
            .append("  <sourceAddress type=\"international\">" + srcAddr + "</sourceAddress>\n")
            .append("  <destinationAddress type=\"network\">40404</destinationAddress>\n")
            .append("  <text encoding=\"ISO-8859-1\">" + HexUtil.toHexString(text.getBytes()) + "</text>\n")
            .append(" </deliverRequest>\n").append("</operation>\n").append("");

    HttpClient client = new DefaultHttpClient();
    client.getParams().setBooleanParameter("http.protocol.expect-continue", false);

    long start = System.currentTimeMillis();

    // execute request
    try {/*from w  w  w .j a  va  2s.c o m*/
        HttpPost post = new HttpPost(URL);

        StringEntity entity = new StringEntity(string0.toString(), "ISO-8859-1");
        entity.setContentType("text/xml; charset=\"ISO-8859-1\"");
        post.setEntity(entity);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        String responseBody = client.execute(post, responseHandler);

        logger.debug("----------------------------------------");
        logger.debug(responseBody);
        logger.debug("----------------------------------------");
    } finally {
        // do nothing
    }

    long end = System.currentTimeMillis();

    logger.debug("Response took " + (end - start) + " ms");

}

From source file:com.cloudhopper.sxmp.SubmitMain.java

static public void main(String[] args) throws Exception {
    String url = "http://localhost:9080/api/sxmp/1.0";

    // create a submit request
    SubmitRequest submit = new SubmitRequest();

    submit.setAccount(new Account("customer1", "password1"));

    submit.setDeliveryReport(Boolean.TRUE);

    MobileAddress sourceAddr = new MobileAddress();
    sourceAddr.setAddress(MobileAddress.Type.NETWORK, "40404");
    submit.setSourceAddress(sourceAddr);

    submit.setOperatorId(24);/*from  w w w  .  j  a va  2 s.c  o m*/

    MobileAddress destAddr = new MobileAddress();
    destAddr.setAddress(MobileAddress.Type.INTERNATIONAL, "+14155551212");

    submit.setDestinationAddress(destAddr);

    submit.setText(
            "Test abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijjabcdefghijjabcdefghijjabcdefghijjabcdefghijjabcdefghijjabcdefghijjabcdefghijbcdefghij",
            TextEncoding.UTF_8);
    //submit.setText("Hello World");

    // Get file to be posted
    HttpClient client = new DefaultHttpClient();

    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    client.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);

    long totalStart = System.currentTimeMillis();
    int count = 1;

    for (int i = 0; i < count; i++) {
        long start = System.currentTimeMillis();

        // execute request
        try {
            HttpPost post = new HttpPost(url);

            //ByteArrayEntity entity = new ByteArrayEntity(data);
            StringEntity entity = new StringEntity(SxmpWriter.createString(submit));
            entity.setContentType("text/xml; charset=\"iso-8859-1\"");
            post.setEntity(entity);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String responseBody = client.execute(post, responseHandler);
            long stop = System.currentTimeMillis();

            logger.debug("----------------------------------------");
            logger.debug("Response took " + (stop - start) + " ms");
            logger.debug(responseBody);
            logger.debug("----------------------------------------");
        } finally {
            // do nothing
        }
    }

    long totalEnd = System.currentTimeMillis();

    logger.debug("Response took " + (totalEnd - totalStart) + " ms for " + count + " requests");

    double seconds = ((double) (totalEnd - totalStart)) / 1000;
    double smspersec = ((double) count) / seconds;
    logger.debug("SMS / Sec: " + DecimalUtil.toString(smspersec, 2));

}

From source file:com.cloudhopper.sxmp.demo.SubmitMain.java

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

    String url = "http://127.0.0.1:8080/api/sxmp/1.0";
    String phone = "+14155551212";
    int operator = 1;
    if (args.length > 0)
        url = args[0];//  www. j a v  a 2 s  .  c  o  m
    if (args.length > 1)
        phone = args[1];
    if (args.length > 2)
        operator = Integer.parseInt(args[2]);

    // create a submit request
    SubmitRequest submit = new SubmitRequest();
    submit.setAccount(new Account("customer1", "password1"));
    submit.setDeliveryReport(Boolean.TRUE);

    MobileAddress sourceAddr = new MobileAddress();
    sourceAddr.setAddress(MobileAddress.Type.NETWORK, "40404");
    submit.setSourceAddress(sourceAddr);
    submit.setOperatorId(operator);

    submit.setPriority(Priority.URGENT);

    MobileAddress destAddr = new MobileAddress();
    destAddr.setAddress(MobileAddress.Type.INTERNATIONAL, phone);

    submit.setDestinationAddress(destAddr);
    submit.setText("Hello World");

    // Get file to be posted
    HttpClient client = new DefaultHttpClient();

    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    client.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);

    long totalStart = System.currentTimeMillis();
    int count = 1;

    for (int i = 0; i < count; i++) {
        long start = System.currentTimeMillis();

        // execute request
        try {
            HttpPost post = new HttpPost(url);

            //ByteArrayEntity entity = new ByteArrayEntity(data);
            StringEntity entity = new StringEntity(SxmpWriter.createString(submit));
            entity.setContentType("text/xml; charset=\"iso-8859-1\"");
            post.setEntity(entity);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String responseBody = client.execute(post, responseHandler);
            long stop = System.currentTimeMillis();

            logger.debug("----------------------------------------");
            logger.debug("Response took " + (stop - start) + " ms");
            logger.debug(responseBody);
            logger.debug("----------------------------------------");
        } finally {
            // do nothing
        }
    }

    long totalEnd = System.currentTimeMillis();

    logger.debug("Response took " + (totalEnd - totalStart) + " ms for " + count + " requests");

    double seconds = ((double) (totalEnd - totalStart)) / 1000;
    double smspersec = ((double) count) / seconds;
    logger.debug("SMS / Sec: " + DecimalUtil.toString(smspersec, 2));

}