调用三方接口

一、创建工具类

package com.msun.fooddisease.Util;

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.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName HttpUtils
 * @Description 请求工具
 * @Author msx
 * @Date 2021-08-26 17:40
 * @Version 1.0
 */
public class HttpUtils {

    /**
     * @Description get请求,参数k-v形式
     * @Author msx
     * @Date 2021-08-26 17:41
     * @param url-[请求地址] headMap-[请求头参数] paramMap-[请求参数]
     * @return String 返回结果
     */
    public static String doGet(String url, Map<String, String> headMap, Map<String, String> paramMap) {
        System.out.println(" = = 请求地址 = = > > > > > > " + url);
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = "";
        CloseableHttpResponse response = null;
        try {
            //创建uri
            URIBuilder builder = new URIBuilder(url);
            if (paramMap != null && !paramMap.isEmpty()) {
                Set<Map.Entry<String, String>> entrySet = paramMap.entrySet();
                for (Map.Entry<String, String> entry : entrySet) {
                    builder.addParameter(entry.getKey(), entry.getValue());
                }
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);

            //添加请求头
            if (headMap != null && !headMap.isEmpty()) {
                Set<Map.Entry<String, String>> entries = headMap.entrySet();
                System.out.println(" = = 请求头 = = > > > > > > ");
                for (Map.Entry<String, String> e : entries) {
                    System.out.println(e.getKey() + ":" + e.getValue());
                    httpGet.addHeader(e.getKey(), e.getValue());
                }
            }

            // 执行请求
            response = httpClient.execute(httpGet);
            // 判断返回状态是否为200
            /*if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
            }*/
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");
            System.out.println(" = = 请求返回 = = > > > > > > " + result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(response, httpClient);
        }
        return result;
    }

    /**
     * @Description post请求,参数k-v形式
     * @Author msx
     * @Date 2021-08-29 21:13
     * @param url-[请求地址] headMap-[请求头参数] paramMap-[请求参数]
     * @return String 返回结果
     */
    public static String doPost(String url, Map<String, String> headMap, Map<String, String> paramMap) {
        System.out.println(" = = 请求地址 = = > > > > > > " + url);
        System.out.println(" = = 请求参数 = = > > > > > > " + paramMap);
        //创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try{
            //创建uri
            URIBuilder builder = new URIBuilder(url);
            if (paramMap != null && !paramMap.isEmpty()) {
                Set<Map.Entry<String, String>> entrySet = paramMap.entrySet();
                for (Map.Entry<String, String> entry : entrySet) {
                    builder.addParameter(entry.getKey(), entry.getValue());
                }
            }
            URI uri = builder.build();
            //创建http请求
            HttpPost httpPost = new HttpPost(uri);
            //添加请求头
            if (headMap != null && !headMap.isEmpty()) {
                System.out.println(" = = 请求头 = = > > > > > > ");
                Set<Map.Entry<String, String>> entries = headMap.entrySet();
                for (Map.Entry<String, String> entry : entries) {
                    System.out.println(entry.getKey() + ":" + entry.getValue());
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            // 执行请求
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(" = = 请求返回 = = > > > > > > " + result);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            closeResource(response, httpClient);
        }
        return result;
    }

    /**
     * @Description post请求,请求体为JSON格式
     * @Author msx
     * @Date 2021-08-27 16:31
     * @param url-[请求地址] headMap-[请求头参数] paramMap-[请求参数]
     * @return String 返回结果
     */
    public static String doPost(String url, Map<String, String> header, String jsonParams){
        System.out.println(" = = 请求地址 = = > > > > > > " + url);
        System.out.println(" = = 请求参数 = = > > > > > > " + jsonParams);
        //创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String result = "";
        try{
            //创建http请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json");
            //创建请求内容
            StringEntity entity = new StringEntity(jsonParams, "utf-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            // 设置请求头
            if (null != header && !header.isEmpty()) {
                System.out.println(" = = 请求头 = = > > > > > > ");
                Set<Map.Entry<String, String>> entries = header.entrySet();
                for (Map.Entry<String, String> e : entries) {
                    System.out.println(e.getKey() + ":" + e.getValue());
                    httpPost.setHeader(e.getKey(), e.getValue());
                }
            }
            response = httpClient.execute(httpPost);
            result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println(" = = 请求返回 = = > > > > > > " + result);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭资源
            closeResource(response, httpClient);
        }
        return result;
    }

    /**
     * @Description 关闭资源
     * @Author msx
     * @Date 2021/9/8 10:44
     */
    private static void closeResource(Closeable ... resources) {
        System.out.println("> > > > > > > > > > 关闭流资源 > > > > > > > > > >");
        try {
            for (Closeable resource : resources) {
                if (resource != null) {
                    resource.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

二、使用工具类

 //调用判断接口
        String url = "http://172.16.0.43/infections/goto/isFoodDiseases";
        log.info("url:{}", url);
        Map<String,String> hashMap = new HashedMap();
        hashMap.put("diagnosisName",diagnosisNameResult);
        String result = HttpUtils.doGet(url, null, hashMap);
        log.info("result:{}", result);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木木的成长之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值