Springboot调用http(https)接口小妙招,新手小白版

有的新手彦祖在搬砖过程中会遇到调用别人接口来获取数据的需求,这其中涉及调用一些相关类及方法的调用,最近干活又要用这个了,把以前的代码搬出来套用下,死活报错协议加密证书啥的问题,真想感叹卑微打工仔挣点钱养家糊口不容易。无所谓,我师傅会出手,过来一眼就看出我的问题,我用的http协议写的,人家接口测试环境是https协议写的,https协议有加密方式,去参照大佬的写法封装整理了两个类。开发差不多完成后在这记录分享下,不多bb直接上源码了。

package com.inspur.dehongtf.controller.http_data_get;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
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.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Map;

public class httpsRequest {

    public JSONObject send2(String url, Map<String, Object> params) {
           HttpHeaders headers = new HttpHeaders();
           headers.setContentType(MediaType.APPLICATION_JSON);
           headers.setAccept(Arrays.asList(MediaType.ALL));
           HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(params), headers);
           try {
                String body = getRestTemplate().postForObject(url, entity, String.class);
                return JSONObject.parseObject(body);
              }

           catch (Exception e) {
                e.printStackTrace();
                               }
           return null;
    }

    public static RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
               SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
               @Override
               public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true;
               }
        }).build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
                new String[]{"TLSv1"},
                null,
                NoopHostnameVerifier.INSTANCE);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}

以上是一个自己再次封装的用于调用https协议的类,send2方法调用了getRestTemplate方法,在测试类中调用时传入两个参数,其一是String类型的url,其二是指定泛型的map集合,也就是post的请求参数,对了,这个方法目前只适用于post方法。

如果有彦祖在看,顺便给彦祖们测试下

这里定义并书写了参数和url,实例化上面的httpsRequest类,传入url和请求参数调用send2方法,得到的结果就是在url接口指定参数下的数据,下面输出看一下

在控制层调用上面类的getParams方法,得到返回数据,启动工程,postman调用自己的接口,输出看一下数据,没问题。

这里我也写了一个http的类,但是好像https这个类能共用http与https,深层原理咱也不知道,http类如下,难免以后找不到了,又被同事说:多删几个妹子,不要删代码。

package com.inspur.dehongtf.controller.http_data_get;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class httpRequest {
    public JSONObject send1(String url, Map<String, Object> params) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        RestTemplate restTemplate = new RestTemplate();
        JSONObject obj = new JSONObject(params);
        String s = obj.toString();
        HttpEntity request = new HttpEntity(s, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class, new Object[0]);
        JSONObject responseData = JSONObject.parseObject((String)responseEntity.getBody());
        return responseData;
}

}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值