http自调util

接口自调 sendGetRequest、sendPostRequest、sendPutRequest和sendDeleteRequest,分别用于发送GET、POST、PUT和DELETE请求。 这些方法都接受两个参数:endpoint表示要访问的API端点路径,requestBody表示可选的请求体数据。这些方法在内部使用Apache HttpClient来发送请求,并返回响应的字符串表示形式。

相关代码如下

private final String BASE_URL = “http://127.0.0.1:8082”; // 基础URL,根据实际情况修改

 public  String sendGetRequest(String endpoint, Map<String,String> params) throws IOException, URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(BASE_URL + endpoint);
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }

        HttpGet httpGet = new HttpGet(uriBuilder.build());
        httpGet.setHeader("//这里封装具体的header,无需要可不封装");
        HttpResponse response = httpClient.execute(httpGet);
        return extractResponseString(response);
    }

    public String sendPostRequest(String endpoint, String requestBody) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(BASE_URL + endpoint);
        setRequestBody(httpPost, requestBody);
        httpPost.setHeader("//这里封装具体的header,无需要可不封装");
        HttpResponse response = httpClient.execute(httpPost);
        return extractResponseString(response);
    }

    public  String sendPutRequest(String endpoint, String requestBody) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(BASE_URL + endpoint);
        httpPut.setHeader("//这里封装具体的header,无需要可不封装");
        setRequestBody(httpPut, requestBody);
        HttpResponse response = httpClient.execute(httpPut);
        return extractResponseString(response);
    }

    public  String sendDeleteRequest(String endpoint,Map<String,String> params) throws IOException, URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(BASE_URL + endpoint);
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                uriBuilder.addParameter(entry.getKey(), entry.getValue());
            }
        }
        HttpDelete httpDelete = new HttpDelete(BASE_URL + endpoint);
        httpDelete.setHeader("//这里封装具体的header,无需要可不封装");
        HttpResponse response = httpClient.execute(httpDelete);
        return extractResponseString(response);
    }

    private void setRequestBody(HttpEntityEnclosingRequestBase request, String requestBody) {
        if (requestBody != null) {
            StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
            request.setEntity(entity);
        }
    }

    private String extractResponseString(HttpResponse response) throws IOException {
        HttpEntity responseEntity = response.getEntity();
        return EntityUtils.toString(responseEntity);
    }

以上仅个人学习使用,有问题也希望指出

  • 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、付费专栏及课程。

余额充值