SpringBoot 集成HttpClientUtil 实现http/https请求

使用HttpClient发起请求

引入jar包

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.7</version>
</dependency>

代码

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpUtils {
     /*
       Http协议GET请求
      */
     public static String httpGet(String url) throws Exception{
         //初始化HttpClient
         CloseableHttpClient httpClient = HttpClients.createDefault();
         //创建HttpGet
         HttpGet httpGet = new HttpGet(url);
         //发起请求,获取response对象
         CloseableHttpResponse response = httpClient.execute(httpGet);
         //获取请求状态码
         //response.getStatusLine().getStatusCode();
         //获取返回数据实体对象
         HttpEntity entity = response.getEntity();
         //转为字符串
         String result = EntityUtils.toString(entity,"UTF-8");
         return result;

     }
     /*
       Http协议Post请求
      */
     public static String httpPost (String url,String json) throws Exception{
         //初始HttpClient
         CloseableHttpClient httpClient = HttpClients.createDefault();
         //创建Post对象
         HttpPost httpPost = new HttpPost(url);
         //设置Content-Type
         httpPost.setHeader("Content-Type","application/json");
         //写入JSON数据
         httpPost.setEntity(new StringEntity(json));
         //发起请求,获取response对象
         CloseableHttpResponse response = httpClient.execute(httpPost);
         //获取请求码
         //response.getStatusLine().getStatusCode();
         //获取返回数据实体对象
         HttpEntity entity = response.getEntity();
         //转为字符串
         String result = EntityUtils.toString(entity,"UTF-8");
         return result;

     }


    public static String httpsGet(String url) throws Exception{
        CloseableHttpClient hp = createSSLClientDefault();
        HttpGet hg = new HttpGet(url);
        CloseableHttpResponse response = hp.execute(hg);
        HttpEntity entity = response.getEntity();
        String content = EntityUtils.toString(entity,"UTF-8");
        hp.close();
        return content;
    }

    public static String httpsPost(String url, String json) throws Exception{

         CloseableHttpClient hp = createSSLClientDefault();
         HttpPost httpPost = new HttpPost(url);
         httpPost.setHeader("Content-Type","application/json");
         httpPost.setEntity(new StringEntity(json));
         CloseableHttpResponse response = hp.execute(httpPost);
         HttpEntity entity = response.getEntity();
         String content = EntityUtils.toString(entity,"UTF-8");
         hp.close();
         return content;
    }


    public static CloseableHttpClient createSSLClientDefault() throws Exception{

        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){
            //信任所有
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        }).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }
}

测试

@Test
    public void contextLoads() {

        try {
            String result = HttpUtils.httpGet("http://localhost:9696/table");
            System.out.println("返回值>>>:"+result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
返回值>>>:[{"columns":[{"field":"TOKEN_ID","type":"int(11)","collation":null,"key":"PRI","comment":"ACCESS_TOKEN 表","null":"NO"},{"field":"ACCESS_TOKE......
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值