[HttpClient]简单使用GET请求

package com.jerry.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @author Jerry
 * @date 2015年2月11日 下午10:58:44
 */
public class FirstDemo {
    public static void main(String[] args) {    
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://www.baidu.com");
        CloseableHttpResponse response = null;
        try {
            response = client.execute(httpGet);
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
            EntityUtils.consume(entity);//关闭httpEntity的的输入流
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();    
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                response = null;
            }
        }
        
    }
}
原文地址:https://www.cnblogs.com/jerry19890622/p/4290916.html