Java--HttpClient请求

Java–HttpClient

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

通过HttpClient可以模拟发出http请求,支持各种请求如get\post\delete等http协议。

也可以做爬虫。

使用Get请求测试一下:

HttpGet请求

通过Get请求获取到响应回来的响应体数据

public void test() throws Exception{
    	//创建一个客户端
        CloseableHttpClient client = HttpClients.createDefault();
		//创建一个Get请求
        HttpGet httpGet = new HttpGet("http://jsonplaceholder.typicode.com/users");
		
    	//通过客户端执行该请求,返回一个响应
        CloseableHttpResponse response = client.execute(httpGet);
		
    	//获取响应体对象
        HttpEntity entity = response.getEntity();
    	
    	//获取响应体内容
        InputStream content = entity.getContent();
        byte[] bytes=new byte[102400];
        int read = content.read(bytes);

    	//输出到控制台
        System.out.println(new String(bytes,0,read));
    
    	//关闭掉所有流
        content.close();
        response.close();
        client.close();
    }


//响应回来的Json数据

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }

  

各种请求都是继承自HttpRequestBase,HttpRequestBase再向上继承。

基本上只是方法名字不同。

@NotThreadSafe
public class HttpGet extends HttpRequestBase {

    public final static String METHOD_NAME = "GET";

    public HttpGet() {
        super();
    }

    public HttpGet(final URI uri) {
        super();
        setURI(uri);
    }

    /**
     * @throws IllegalArgumentException if the uri is invalid.
     */
    public HttpGet(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }

}

爬虫

HttpClient也可以用于爬虫,如:

将获取的网络资源输出到文件中。

//传入url,以及资源路径
public static void getUrl(String url,String path) throws Exception{
        CloseableHttpClient client = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(url);

        CloseableHttpResponse response = client.execute(httpGet);

        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
	
    	//打印流
        PrintStream printStream=new PrintStream(path);
        int len;

        while((len=inputStream.read())!=-1){
            printStream.write(len);
        }

        inputStream.close();
        printStream.close();
        response.close();
        client.close();

    }

    @Test
    public void test2() throws Exception{
        getUrl("http://jsonplaceholder.typicode.com/users","./get.json");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值