发送post请求token及get请求接口

基于http发送post请求获取token接口的工具类,返回的json格式字符串。

重点:只需要修改map中的参数值,和tokenUrl就行

@Component
/**
*发送get/post请求接口
*/
public class SendRequestUtil {
	//导入日志
    private static final Logger log = LoggerFactory.getLogger(SendRequestUtil.class);
    /**
     * @param RequestConfig 连接超时配置
     */
    private static RequestConfig requestConfig;
    static{
        requestConfig = RequestConfig.custom()
            // 设置连接超时时间(单位毫秒)
            .setConnectTimeout(40000)
            // 设置请求超时时间(单位毫秒)
            .setConnectionRequestTimeout(30000)
            // sock读写超时时间
            .setSocketTimeout(20000)
            // 设置是否允许重定向
            .setRedirectsEnabled(true).build();
    }
    /**
     * @return  Post请求接口 响应Token信息
     */
    public String sendPost() {
    //请求参数Key/value	放入map集合
        Map<String, String> tokenParam = new HashMap<>();
        tokenParam.put(usernameKey,usernameVal);	
        tokenParam.put(passwordKey,passwordVal);
        tokenParam.put(clientIdKey,clientIdVal);
        tokenParam.put(clientSecretKey,clientSecretVal);
        tokenParam.put(grantTypeKey,grantTypeVal);
//        打印参数日志
//        for(String key:tokenParam.keySet()){
//            log.info(("key--"+key+" | "+"Value--"+tokenParam.get(key)));
//        }
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(tokenUrl);
            // 创建参数列表
            if (tokenParam != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : tokenParam.keySet()) {
                    paramList.add(new BasicNameValuePair(key, tokenParam.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                httpPost.setEntity(entity);
                httpPost.setConfig(requestConfig);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            //将json转换对象存入responseToKens实体类方便调用,实体类是接口响应过来的参数写的
            responseTokens = JSON.parseObject(result, ResponseToken.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
//类路径
import com.fasterxml.jackson.annotation.JsonProperty;
public class ResponseToken {
	//@JsonProperty(value="access_token")作用于序列化属性名为另外一个名称
    @JsonProperty(value="access_token")
    private String accessToken;
    @JsonProperty(value="expires_in")
    private int expiresIn;
    @JsonProperty(value="token_type")
    private String tokenType;
    @JsonProperty(value="refresh_token")
    private String refreshToken;
    private String scope;
    //Get/Set方法
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值