集册 Java实例教程 使用apache http的客户端连接发布

使用apache http的客户端连接发布

欢马劈雪     最近更新时间:2020-01-02 10:19:05

527
使用apache http的客户端连接发布


import java.io.BufferedReader;/* 来自 时代Java - N o w  J a v a . c o m*/

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("------------------------------------");/* 来自 n o w j a v a . c o m - 时  代  Java*/


            HttpEntity entity = res.getEntity();


            if (entity != null) {

                BufferedReader in = new BufferedReader(

                        new InputStreamReader(entity.getContent()));

      
展开阅读全文