美文网首页
java 发送Bearer token请求

java 发送Bearer token请求

作者: 那钱有着落吗 | 来源:发表于2021-08-17 15:21 被阅读0次

发送一般的token使用下面这种即可:

 HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept", "application/json");
httpHeaders.add("Authorization", propertyUtils.getDeviceTreeToken());
String result = getRequest(propertyUtils.getDeviceTreeUrl(), null, httpHeaders);





 private String getRequest(String url, MultiValueMap<String, String> params, HttpHeaders headers) throws Exception {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(0);// 设置建立连接超时时间
        requestFactory.setReadTimeout(0);// 设置等待返回超时时间

        RestTemplate client = new RestTemplate(requestFactory);
        HttpMethod method = HttpMethod.GET;
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

针对于Bearer token改下写法就行:

            httpHeaders.add("Authorization", "Bearer " + propertyUtils.getDeviceTreeToken());

相关文章

网友评论

      本文标题:java 发送Bearer token请求

      本文链接:https://www.haomeiwen.com/subject/vlhibltx.html