HttpClient配置及运用

Posted IToIT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient配置及运用相关的知识,希望对你有一定的参考价值。

  HttpComponents是Apache 旗下的项目。其中有一个HttpClient,即HTTP客户端。

之后我们建立项目,导入jar包,开始开发

  一、获取网页内容:

package com.zhi.httpClient;
 
import java.io.IOException;
 
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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;
 
public class Demo {
    public static void main(String[] args) {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        //CloseableHttpClient意思是:可关闭的
        HttpGet httpGet=new HttpGet("http://www.huimsoft.com"); //2、创建实例
         
        //限制类型http://www.tuicool.com/
         
        CloseableHttpResponse closeableHttpResponse=null;
        try {
            closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        } catch (ClientProtocolException e) { //客户端协议异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) { //IO异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
 
        String str="";
        try {
            str=EntityUtils.toString(httpEntity, "utf-8"); //获取网页内容,并且指定编码
        } catch (ParseException e) { //解析异常
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(str);
         
        //关闭closeableHttpResponse
        try {
            closeableHttpResponse.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        //关闭closeableHttpClient
        try {
            closeableHttpClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

  代码有很多,但是很多是在处理异常,所以我们也可以使用throws在方法上抛出,以简化代码格式,让其更好看。

  

package com.zhi.httpClient;
 
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;
 
public class Test {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://www.huimsoft.com"); //2、创建请求
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //5、获取网页内容,并且指定编码
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

  二、设置请求头信息

 设置请求头信息的目的是为了模拟浏览器

  

package com.zhi.httpClient2;
 
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;
 
public class Demo {
 
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); //2、创建请求
         
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
        System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //5、获取网页内容,并且指定编码
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
 
}

三、获取相应头信息Response Headers的内容类型Content-type

package com.zhi.httpClient2;
 
import org.apache.http.Header;
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;
 
public class Demo {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://www.tuicool.com"); //2、创建请求
         
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
         
        Header header=httpEntity.getContentType();
        System.out.println(header.toString()); //获取头信息内容
        System.out.println(header.getName().toString()); //这是获取key
        System.out.println(header.getValue().toString()); //这是获取value
         
        //System.out.println(EntityUtils.toString(httpEntity, "utf-8")); //5、获取网页内容,并且指定编码
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

运行输出:

Content-Type: text/html; charset=utf-8
Content-Type
text/html; charset=utf-8

我们的重点是获取的value的值,有些是text/html的,有些是其他的,比如我们请求一个js文件。

https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js

 

HttpGet httpGet=new HttpGet("http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js");
public class Demo {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient closeableHttpClient=HttpClients.createDefault(); //1、创建实例
        HttpGet httpGet=new HttpGet("http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"); //2、创建实例
         
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
         
        CloseableHttpResponse closeableHttpResponse=closeableHttpClient.execute(httpGet); //3、执行
        HttpEntity httpEntity=closeableHttpResponse.getEntity(); //4、获取实体
         
        Header header=httpEntity.getContentType();
        System.out.println(header.getValue().toString()); //这是获取value
         
        closeableHttpResponse.close();
        closeableHttpClient.close();
    }
}

运行结果:

运行结果:application/javascript

如果请求一个图片,同上

运行结果:image/png

以上是关于HttpClient配置及运用的主要内容,如果未能解决你的问题,请参考以下文章

HttpClient配置及示例代码

HttpClient 4.3连接池参数配置及源码解读

轻松把玩HttpClient之封装HttpClient工具类,插件式配置HttpClient对象

HttpClient的简单运用

一.HttpClientJsonPathJsonObject运用

接口-httpClient