httpclient使用实例

3 篇文章 0 订阅

httpclient学习文档

一、 httpclient 简介

官网: Apache HttpComponents – Apache HttpComponents 使用版本: 4.5.13

使用场景:

爬虫、多系统之间接口交互

二、JDK 原生api发送http请求

2.1 HttpURLConnection

import org.junit.jupiter.api.Test;
​
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
​
public class HttpURLCOnnTest {
​
​
    /**
     * 使用 jdk原生的API来请求网页
     * @throws Exception
     */
    @Test
    public void jdkapi() throws Exception{
        String urlStr = "https://www.baidu.com";
        URL url = new URL(urlStr);
        URLConnection urlConnection = url.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        //设置请求类型
      // 请求行、 空格、 请求头、 请求体
        httpURLConnection.setRequestMethod("GET");
        //请求头属性
        httpURLConnection.setRequestProperty("Accept-Charset","utf-8");
       //获取httpURLConnection 的输入流
        try(
                InputStream is = httpURLConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
         ){
            String line;
            while((line = br.readLine())!=null){
                System.out.println(line);
            }
        }
    }
​
}
​

三、发送get请求

pom 依赖

     <!--httpclient依赖-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

无参get请求

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;
import org.junit.jupiter.api.Test;
​
import java.io.IOException;
import java.nio.charset.StandardCharsets;
​
public class HttpClientTest {
​
​
    /**
     * 使用httpclient发送get请求
     */
​
    @Test
    public void testGet1(){
     //可关闭的httpclient客户端,相当于你打开一个浏览器
     CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
     String urlStr = "https://www.baidu.com";
     //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlStr);
        //可关闭的响应
        CloseableHttpResponse response =null;
        try{
            response = closeableHttpClient.execute(httpGet);
            //获取响应结果: DecompressingEntity,不仅可以作为结果,也可以作为参数实体,有很多实现
            HttpEntity entity =response.getEntity();
            //对HttpEntity操作的工具类
            String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            System.out.println(toStringResult);
            //确保流关闭
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
​
        }finally {
            if(closeableHttpClient!=null){
                try{
                    closeableHttpClient.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try{
                    response.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
 

伪装成浏览器:请求头的作用,防盗链

//加请求头, 解决http请求不认为真实请求
httpGet.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36");
//加防盗链,vaule:要是发生防盗链网站的url
httpGet.addHeader("Referer","https://www.baidu.com/");

有参数: 注意urlencode

获取响应头以及相应的content-type

下载网络图片到本地

/**
     * 获取网络图片并保存到本地
     */
    @Test
    public void downloadPicture() throws Exception{
​
        //可关闭的httpclient客户端,相当于你打开一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
​
        String urlStr = "https://pics0.baidu.com/feed/4b90f603738da977ab03b9e0731a501f8718e39b.jpeg?token=47f4e2bbdd4a1fe91303ae4679f70a2d";
        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlStr);
​
       //可关闭的响应
        CloseableHttpResponse response =null;
        try{
                 response = closeableHttpClient.execute(httpGet);
                 
                HttpEntity entity =response.getEntity();
                String contentType = entity.getContentType().getValue();
                String suffix = ".jpg";
                if (contentType.contains("jpg")||contentType.contains("jpeg")){
                    suffix=".jpg";
                }else  if(contentType.contains("bmp")||contentType.contains("bitmap")){
                    suffix=".bmp";
                }else  if(contentType.contains("png")){
                    suffix=".png";
                }else  if(contentType.contains("gif")){
                    suffix=".gif";
                }
​
                //将entity转换为字节流
                byte[] bytes = EntityUtils.toByteArray(entity);
                //定义文件下载路径
                String localAbsPath = "e:\\picture"+suffix;
                FileOutputStream fos = new FileOutputStream(localAbsPath);
                fos.write(bytes);
                fos.close();
                //确保流关闭
                EntityUtils.consume(entity);
​
        } catch (Exception e) {
            e.printStackTrace();
​
        }finally {
            if(closeableHttpClient!=null){
                try{
                    closeableHttpClient.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try{
                    response.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

设置网络代理

/**
     * 设置访问代理
     * @throws Exception
     */
    @Test
    public void requestProxy() throws Exception {
        //可关闭的httpclient客户端,相当于你打开一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlStr = "https://www.baidu.com";
        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlStr);
        //创建一个代理,可使用http://www.66ip.cn/ 寻找免费代理IP
        String ip ="96.113.165.182";
        int port=3128;
        HttpHost proxy = new HttpHost(ip,port);
        //对每一个请求进行配置,会覆盖全局的默认请求配置
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        httpGet.setConfig(requestConfig);
       //可关闭的响应
        CloseableHttpResponse response =null;
        try{
               response = closeableHttpClient.execute(httpGet);
​
                //获取响应结果: DecompressingEntity,不仅可以作为结果,也可以作为参数实体,有很多实现
                HttpEntity entity =response.getEntity();
                //对HttpEntity操作的工具类
                String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                System.out.println(toStringResult);
                //确保流关闭
                EntityUtils.consume(entity);
​
        } catch (Exception e) {
            e.printStackTrace();
​
        }finally {
            if(closeableHttpClient!=null){
                try{
                    closeableHttpClient.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try{
                    response.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
​

设置连接超时和读取超时

 /**
     * 设置连接超时和读取超时
     * @throws Exception
     */
    @Test
    public void setConnectTimeOut() throws Exception {
        //可关闭的httpclient客户端,相当于你打开一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlStr = "https://www.baidu.com";
        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlStr);
        //创建一个代理,可使用http://www.66ip.cn/ 寻找免费代理IP
        String ip ="96.113.165.182";
        int port=3128;
        HttpHost proxy = new HttpHost(ip,port);
        //对每一个请求进行配置,会覆盖全局的默认请求配置
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000)   //连接超时, ms, 完成tcp3次握手的时间上限
                .setSocketTimeout(3000)   //读取超时,ms, 表示从请求的网址处获得响应数据的时间间隔
                .setConnectionRequestTimeout(5000) //从连接池里面获取connect的超时时间
                .build();
        httpGet.setConfig(requestConfig);
        //可关闭的响应
        CloseableHttpResponse response =null;
        try{
            response = closeableHttpClient.execute(httpGet);
​
            //获取响应结果: DecompressingEntity,不仅可以作为结果,也可以作为参数实体,有很多实现
            HttpEntity entity =response.getEntity();
            //对HttpEntity操作的工具类
            String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            System.out.println(toStringResult);
            //确保流关闭
            EntityUtils.consume(entity);
​
        } catch (Exception e) {
            e.printStackTrace();
​
        }finally {
            if(closeableHttpClient!=null){
                try{
                    closeableHttpClient.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try{
                    response.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

四、发送post请求

使用httpclient发送application/x-www-form-urlencode类型的post请求

  
 /**
     * 使用httpclient发送application/x-www-form-urlencode类型的post请求
     */
​
    @Test
    public void postWithForm(){
​
        //可关闭的httpclient客户端,相当于你打开一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlStr = "https://www.baidu.com";
        //构造httpGet请求对象
        HttpPost httpPost = new HttpPost(urlStr);
​
        //给post对象设置参数
        List<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("username","post"));
        list.add(new BasicNameValuePair("password","123"));
        //把参数集合设置到formEntity
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);
        //enetity设置内容类型
        formEntity.setContentType(new BasicHeader("Content-Type","application/x-www-form-urlencode"));
        httpPost.setEntity(formEntity);
        //可关闭的响应
        CloseableHttpResponse response =null;
        try{
            response = closeableHttpClient.execute(httpPost);
            //获取响应结果: DecompressingEntity,不仅可以作为结果,也可以作为参数实体,有很多实现
            HttpEntity entity =response.getEntity();
            //对HttpEntity操作的工具类
            String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            System.out.println(toStringResult);
            //确保流关闭
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
​
        }finally {
            if(closeableHttpClient!=null){
                try{
                    closeableHttpClient.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try{
                    response.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
​
​

使用httpclient发送application/json类型的post请求

 
 /**
     * 使用httpclient发送application/json类型的post请求
     */
​
    @Test
    public void postWithJson(){
​
        //可关闭的httpclient客户端,相当于你打开一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlStr = "https://www.baidu.com";
        //构造httpGet请求对象
        HttpPost httpPost = new HttpPost(urlStr);
​
        //给post对象设置参数
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userName","java");
        jsonObject.put("password","123");
        StringEntity jsonEntity = new StringEntity(jsonObject.toString(),Consts.UTF_8);
​
        //enetity设置内容类型
        jsonEntity.setContentType(new BasicHeader("Content-Type","application/json"));
        //设置entity编码
        jsonEntity.setContentEncoding(Consts.UTF_8.name());
        httpPost.setEntity(jsonEntity);
        //可关闭的响应
        CloseableHttpResponse response =null;
        try{
            response = closeableHttpClient.execute(httpPost);
            //获取响应结果: DecompressingEntity,不仅可以作为结果,也可以作为参数实体,有很多实现
            HttpEntity entity =response.getEntity();
            //对HttpEntity操作的工具类
            String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            System.out.println(toStringResult);
            //确保流关闭
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
​
        }finally {
            if(closeableHttpClient!=null){
                try{
                    closeableHttpClient.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(response!=null){
                try{
                    response.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
​

表单上传文件

AJAX的post请求与上传文件_端碗吹水的技术博客_51CTO博客_ajax上传文件

五、请求https链接

安全: https

不安全: http

解决方法:1、配置认证需要的秘钥配置httpclient

2、 配置httpclient绕过https安全认证

六、使用httpclient连接池

性能,提高程序的运行速度

连接的可复用性

七、封装通用的httpclientUtil

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
HttpClient 是一个开源的 HTTP 客户端库,用于发送 HTTP 请求和接收 HTTP 响应。它提供了简化的 API,使得进行 HTTP 通信变得更加容易。 要使用 HttpClient,首先需要在项目中引入 HttpClient 的依赖。具体的操作方式取决于你使用的开发环境和构建工具。一般来说,如果是使用 Maven 进行项目管理,可以在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ``` 如果是使用 Gradle 进行项目管理,可以在 build.gradle 文件中添加以下依赖: ```groovy implementation 'org.apache.httpcomponents:httpclient:4.5.13' ``` 接下来就可以在代码中使用 HttpClient 来发送 HTTP 请求了。下面是一个简单的示例: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; public class HttpClientExample { public static void main(String[] args) throws Exception { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet request = new HttpGet("http://example.com"); HttpResponse response = httpClient.execute(request); System.out.println("Response Code: " + response.getStatusLine().getStatusCode()); } } ``` 上述示例中,我们创建了一个 HttpClient 实例,并使用实例发送了一个 GET 请求到 "http://example.com"。获取到的响应存储在 HttpResponse 对象中,我们可以通过调用 `getStatusCode()` 方法获取响应的状态码。 当然,HttpClient 还提供了丰富的 API,可以进行更加复杂的 HTTP 请求和处理。你可以参考 HttpClient 的官方文档来了解更多详细的使用方法和示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九城风雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值