HttpClientUtil工具类

项目中常使用第三方工具或应用,如小程序、公众号、腾讯云等,需向其 API 发送请求实现功能。可将发送请求的不同操作封装成工具类,以便调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中,我们经常会使用到第三方的一些工具或者应用,如小程序,公众号,腾讯云等。需要对第三方相应的 API 发送请求,以实现具体的功能,因此我们可以把发送请求的不同操作封装成一个工具类,方便调用。


		public class HttpClientUtil{
		
		    private CloseableHttpClient client = null;
		
		    public HttpClientUtil() {
		        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(7000).setSocketTimeout(7000).build();
		        client = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
		    }
		
		    public String post(String url) throws ParseException, ClientProtocolException, IOException {
		        return post(url, null);
		    }
		
		    public String post(String url, String params) throws ParseException, ClientProtocolException, IOException {
		        HttpPost post = new HttpPost(url);
		
		        if (StringUtils.isNotEmpty(params)) {
		            HttpEntity entity = new StringEntity(params,"UTF_8");
		            post.setEntity(entity);
		        }
		        CloseableHttpResponse response = client.execute(post);
		        try {
		            return EntityUtils.toString(response.getEntity(), "UTF_8");
		        } finally {
		            response.close();
		        }
		    }

			public String postJson(String url, String params) throws ParseException, ClientProtocolException, IOException {
		        HttpPost post = new HttpPost(url);
	
		        if (StringUtils.isNotEmpty(params)) {
		            StringEntiy entity = new StringEntity(params,"UTF_8");
		            post.setHeader("Content-type", "application/json");//设置请求头类型
		            post.setEntity(entity);
		        }
		        CloseableHttpResponse response = client.execute(post);
		        try {
		            return EntityUtils.toString(response.getEntity(), "UTF_8");
		        } finally {
		            response.close();
		        }
		    }
		
		    public String get(String url) throws ParseException, ClientProtocolException, IOException {
		        HttpGet get = new HttpGet(url);
		        CloseableHttpResponse response = client.execute(get);
		        try {
		            return EntityUtils.toString(response.getEntity(), Constants.CS_UTF_8);
		        } finally {
		            response.close();
		        }
		    }
		
		    public String getWithRequestConfig(String url,
		                                       RequestConfig requestConfig) throws ParseException, ClientProtocolException, IOException {
		        HttpGet get = new HttpGet(url);
		        get.setConfig(requestConfig);
		        CloseableHttpResponse response = client.execute(get);
		        try {
		            return EntityUtils.toString(response.getEntity(), Constants.CS_UTF_8);
		        } finally {
		            response.close();
		        }
		    }
		
		    /**
		     * 需要调用者关闭 Response
		     * 
		     * @param url
		     * @return
		     * @throws Exception
		     */
		    public CloseableHttpResponse getResponse(String url) throws IOException, ClientProtocolException {
		        HttpGet get = new HttpGet(url);
		        CloseableHttpResponse response = client.execute(get);
		        return response;
		    }
		
		    /**
		     * 需要调用者关闭 Response
		     * 
		     * @param url
		     * @return
		     * @throws Exception
		     */
		    public CloseableHttpResponse postResponse(String url) throws IOException, ClientProtocolException {
		        HttpPost post = new HttpPost(url);
		        CloseableHttpResponse response = client.execute(post);
		        return response;
		    }
		
		}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值