调用第三方接口,发送get,post请求HttpClient 库

本文介绍了SpringBlade框架中HttpClientUtils类提供的发送GET和POST请求的方法,包括如何构造URL、添加参数以及处理响应结果。
摘要由CSDN通过智能技术生成
工具类
package org.springblade.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {
	public static String sendGetRequest(String url, Map<String, String> parameters) throws URISyntaxException, IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		URIBuilder uriBuilder = new URIBuilder(url);
		for (Map.Entry<String, String> entry : parameters.entrySet()) {
			uriBuilder.addParameter(entry.getKey(), entry.getValue());
		}
		URI uri = uriBuilder.build();
		HttpGet httpGet = new HttpGet(uri);
		HttpResponse response = httpClient.execute(httpGet);
		HttpEntity entity = response.getEntity();
		String result = EntityUtils.toString(entity);
		httpClient.close();
		return result;
	}

	public static String sendPostRequest(String url, Map<String, String> parameters) throws IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> params = new ArrayList<>();
		for (Map.Entry<String, String> entry : parameters.entrySet()) {
			params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		httpPost.setEntity(new UrlEncodedFormEntity(params));
		HttpResponse response = httpClient.execute(httpPost);
		HttpEntity entity = response.getEntity();
		String result = EntityUtils.toString(entity);
		httpClient.close();
		return result;
	}
}

发送 GET 请求的示例代码如下

try {
    String url = "http://api.example.com/getData";
    Map<String, String> parameters = new HashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", "value2");
    String response = HttpClientUtils.sendGetRequest(url, parameters);
    System.out.println(response);
} catch (Exception e) {
    e.printStackTrace();
}

发送 POST 请求的示例代码如下

try {
    String url = "http://api.example.com/submitData";
    Map<String, String> parameters = new HashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", "value2");
    String response = HttpClientUtils.sendPostRequest(url, parameters);
    System.out.println(response);
} catch (Exception e) {
    e.printStackTrace();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值