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 HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:se.vgregion.pubsub.inttest.SubscriberRunner.java

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

    LocalTestServer server = new LocalTestServer(null, null);

    server.register("/*", new HttpRequestHandler() {
        @Override/* www  .j  ava 2  s.c  o  m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            String challenge = getQueryParamValue(request.getRequestLine().getUri(), "hub.challenge");

            if (challenge != null) {
                // subscription verification, confirm
                System.out.println("Respond to challenge");
                response.setEntity(new StringEntity(challenge));
            } else if (request instanceof HttpEntityEnclosingRequest) {
                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                //                    System.out.println(HttpUtil.readContent(entity));
            } else {
                System.err.println("Unknown request");
            }
        }
    });
    server.start();

    HttpPost post = new HttpPost("http://localhost:8080/");

    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("hub.callback", buildTestUrl(server, "/").toString()));
    parameters.add(new BasicNameValuePair("hub.mode", "subscribe"));
    parameters.add(new BasicNameValuePair("hub.topic", "http://feeds.feedburner.com/protocol7/main"));
    parameters.add(new BasicNameValuePair("hub.verify", "sync"));

    post.setEntity(new UrlEncodedFormEntity(parameters));

    DefaultHttpClient client = new DefaultHttpClient();
    client.execute(post);
}

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

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://targethost/homepage");

    HttpResponse response1 = httpclient.execute(httpGet);

    // The underlying HTTP connection is still held by the response object 
    // to allow the response content to be streamed directly from the network socket. 
    // In order to ensure correct deallocation of system resources 
    // the user MUST either fully consume the response content  or abort request 
    // execution by calling HttpGet#releaseConnection().

    try {//from  ww  w  . j  av  a2s  . com
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity1);
    } finally {
        httpGet.releaseConnection();
    }

    HttpPost httpPost = new HttpPost("http://targethost/login");
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("username", "vip"));
    nvps.add(new BasicNameValuePair("password", "secret"));
    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
    HttpResponse response2 = httpclient.execute(httpPost);

    try {
        System.out.println(response2.getStatusLine());
        HttpEntity entity2 = response2.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        EntityUtils.consume(entity2);
    } finally {
        httpPost.releaseConnection();
    }
}

From source file:RestGetClient.java

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet("http://localhost:10080/example/json/product/get");
    getRequest.addHeader("accept", "application/json");

    HttpResponse response = httpClient.execute(getRequest);

    //if (response.getStatusLine().getStatusCode() != 200) {
    //   throw new RuntimeException("Failed : HTTP error code : "
    //      + response.getStatusLine().getStatusCode());
    //}/*from w w  w. ja  v  a2  s  .c  o m*/

    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();
}

From source file:httpclientsample.HttpClientSample.java

/**
 * @param args the command line arguments
 *///from w w w . j av a  2s . c om
public static void main(String[] args) throws IOException, JSONException {
    /** METHOD GET EXAMPLE **/
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet("http://localhost:8000/test/api?id=100");
    getRequest.addHeader("accept", "application/json");

    HttpResponse response = httpClient.execute(getRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }

    InputStreamReader isr = new InputStreamReader((response.getEntity().getContent()));
    BufferedReader br = new BufferedReader(isr);

    String output;
    System.out.println("Response:\n");

    while ((output = br.readLine()) != null) {
        JSONObject jsonObj = new JSONObject(output);
        System.out.println("json id : " + jsonObj.get("id"));
    }

    httpClient.getConnectionManager().shutdown();
}

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

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

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();/*w  ww  . j a  v  a 2s  . c o  m*/
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" + "org=self_registered_users&"
            + "goto=/portal/dt&" + "gotoOnFail=/portal/dt?error=true");

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("IDToken1", "username"));
    nvps.add(new BasicNameValuePair("IDToken2", "password"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = httpclient.execute(httpost);
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    // 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.ClientFormLogin.java

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

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/* w  ww. j a  v a2  s . c o  m*/
        HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        //??
        EntityUtils.consume(entity);

        System.out.println("?cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?"
                + "org=self_registered_users&" + "goto=/portal/dt&" + "gotoOnFail=/portal/dt?error=true");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("IDToken1", "username"));
        nvps.add(new BasicNameValuePair("IDToken2", "password"));

        //?
        httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

        response = httpclient.execute(httpost);
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

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

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

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

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*from  ww  w .  j a va 2s . c  o  m*/
        HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?"
                + "org=self_registered_users&" + "goto=/portal/dt&" + "gotoOnFail=/portal/dt?error=true");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("IDToken1", "username"));
        nvps.add(new BasicNameValuePair("IDToken2", "password"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

    } 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:ch.tatool.app.service.export.DataImportTest.java

public static void main(String[] args) {
    // get the httpentity containing the data
    HttpEntity httpEntity = getHttpEntity();
    // Create a httpclient instance
    DefaultHttpClient httpclient = new DefaultHttpClient();
    // setup a POST call
    HttpGet httpGet = new HttpGet(serverUrl);

    try {/*from www.j  a va2 s  .  c o  m*/
        HttpResponse response = httpclient.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            System.out.println("Unable to download data: " + response.getStatusLine().getReasonPhrase());
            System.out.println(response.getStatusLine().getStatusCode());
        } else {
            // all ok
            HttpEntity enty = response.getEntity();
            InputStream is = enty.getContent();

            XmlBeanFactory beanFactory = null;
            try {

                beanFactory = new XmlBeanFactory(new InputStreamResource(is));

            } catch (BeansException be) {
                // TODO: inform user that training configuration is broken
                throw new RuntimeException("Unable to load module configuration", be);
            }

            // check whether we have the mandatory beans (rootElement)
            if (!beanFactory.containsBean("rootElement")) {
                // TODO: inform user that training configuration is broken
                throw new RuntimeException("No rootElement bean found in the module configuration file");
            }

            // fetch the rootElement
            Node rootElement = (Node) beanFactory.getBean("rootElement");

            System.out.println(rootElement.getId());

        }
    } catch (IOException ioe) {

    }

    // 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.da.img.ClientFormLogin.java

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

    DefaultHttpClient httpclient = new DefaultHttpClient();

    try {//  w w w .j av  a2s .  c  o m
        HttpGet httpget = new HttpGet("http://www.soraven.info/index.php");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        //System.out.println( EntityUtils.toString(entity));
        System.out.println("Login form get: " + response.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("-1 " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("http://www.soraven.info/common/include/login.php");
        Header header1 = new BasicHeader("Accept",
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,application/msword, */*");
        Header header2 = new BasicHeader("Referer", "http://www.soraven.info/index.php");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("p_userid", "bimohani"));
        nvps.add(new BasicNameValuePair("p_passwd", "cw8904"));
        nvps.add(new BasicNameValuePair("x", "12"));
        nvps.add(new BasicNameValuePair("y", "20"));
        httpost.setHeader(header1);
        httpost.setHeader(header2);
        httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

        //Thread.sleep(2000);
        response = httpclient.execute(httpost);
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());

        System.out.println(EntityUtils.toString(entity));
        EntityUtils.consume(entity);

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("-2 " + cookies.get(i).toString());
            }
        }

    } 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.openiot.gsn.http.rest.RemoteWrapperHttpClient.java

public static void main(String[] args) throws ClientProtocolException, IOException, ClassNotFoundException {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    final String url = "select * from localsystemtime where timed > 10/10";

    HttpGet httpget = new HttpGet(
            "http://localhost:22001/streaming/" + URLEncoder.encode(url, "UTF-8") + "/12345");
    HttpResponse response = httpclient.execute(httpget);

    ObjectInputStream out = (StreamElement4Rest.getXstream())
            .createObjectInputStream((response.getEntity().getContent()));
    DataField[] structure = (DataField[]) out.readObject();
    StreamElement4Rest se = null;//w w w .  ja  v a  2  s.c  o  m
    try {
        while ((se = (StreamElement4Rest) out.readObject()) != null) {
            System.out.println(se.toStreamElement());
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}