Client Connection Release using apache http - Java Network

Java examples for Network:apache http

Description

Client Connection Release using apache http

Demo Code


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class ClientConnectionRelease {

    public static void main(String[] args) {
        HttpGet httpget = new HttpGet("https://www.google.com");
        System.out.println("executing reqeust " + httpget.getURI());

        try (final CloseableHttpClient hc = HttpClients.createDefault();) {
            HttpResponse res = hc.execute(httpget);
            System.out.println("------------------------------------");
            System.out.println(res.getStatusLine());
            System.out.println("------------------------------------");

            HttpEntity entity = res.getEntity();

            if (entity != null) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(entity.getContent()));
                String line = null;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }/*from  ww w. j  a  v a2  s  .c o m*/
            }

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

}

Related Tutorials