首页 > Web开发 > 详细

[HttpClient]简单使用GET请求

时间:2015-02-13 22:21:54      阅读:346      评论:0      收藏:0      [点我收藏+]
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;
            }
        }
        
    }
}

 

[HttpClient]简单使用GET请求

原文:http://www.cnblogs.com/jerry19890622/p/4290916.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!