第三方接口调用httpUtils

世上唯一不能复制的是时间,唯一不能重演的是人生,唯一不劳而获的是年龄。该怎么走,过什么样的生活,全凭自己的选择和努力。人生很贵,请别浪费!与智者为伍,与良善者同行。第三方接口调用httpUtils,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1.GET 请求

 public static JSONObject getHttpGetResp(String url, String authorization, String title) {
        HttpGet httpGet = new HttpGet(url);
        String resp = getHttpResp(httpGet,authorization, title);
        return StringUtils.isEmpty(resp) ? null : JSONObject.parseObject(resp);
 }

2. POST 请求

 public static JSONObject getHttpPostResp(String url, String authorization, StringEntity strEntity) {
        HttpPost httpPost = new HttpPost(url);
        if (Objects.nonNull(strEntity)) {
            httpPost.setEntity(strEntity);
        }
        String resp = getHttpResp(httpPost,authorization, "");
        return StringUtils.isEmpty(resp) ? null : JSONObject.parseObject(resp);
 }

3.  PATCH 请求

 public static String getHttpPatchResp(String url, String authorization, StringEntity strEntity){
        HttpPatch httpPatch = new HttpPatch(url);
        if (Objects.nonNull(strEntity)) {
            httpPatch.setEntity(strEntity);
        }
        return getHttpResp(httpPatch, authorization, "");
}

4. DELETE 请求

 public static String getHttpDelResp(String url, String authorization, StringEntity strEntity){
        HttpDelete httpDelete = new HttpDelete(url);
        return getHttpResp(httpDelete, authorization, "");
    }

5. PUT 请求

 public static String getHttpPutResp(String url, String authorization, StringEntity strEntity){
        HttpPut httpPut = new HttpPut(url);
        if (Objects.nonNull(strEntity)) {
            httpPut.setEntity(strEntity);
        }
        return getHttpResp(httpPut,authorization, "");
    }

6. 请求方法

 private static String getHttpResp(HttpUriRequest var, String authorization, String text){
        HttpClient client = HttpClients.createDefault();
        var.setHeader("Authorization", authorization);
        var.setHeader("Content-type", MediaType.APPLICATION_JSON_UTF8_VALUE);
        String resp = null;
        try {
            HttpResponse response = client.execute(var);
            if (Objects.nonNull(response)) {
                HttpEntity entity = response.getEntity();
                if (Objects.nonNull(entity)) {
                    resp = EntityUtils.toString(entity, "utf-8");
                    log.info("{}结果:{}", text, resp);
                }
            }
        } catch (Exception e) {
            log.error("{}请求异常", agenda, e);
        }
        return resp;
 }

7、使用restTemplate其他请求

String[] array = list.stream().map(Dev::getS).toArray(String[]::new);
HttpHeaders httpHeader = new HttpHeaders();
httpHeader.setAll(sign);
httpHeader.setContentType(MediaType.APPLICATION_JSON_UTF8);
Map<String, Object> map = new HashMap<>();
map.put(
"list", array); HttpEntity<Map<String, Object>> request = new HttpEntity<>(map, httpHeader); String res = restTemplate.postForObject(URL + "/api/xxx", request, String.class); Msg body = JSONObject.parseObject(res, Msg.class);

private Map<String, String> sign() {
HashMap<String, String> map = Maps.newHashMap();
map.put("Authorization", auth.toString());
map.put("X-Xxx", hdate);

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/260306.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!