java发送httpGet请求和httpPost请求(1)

方式一:通过CloseableHTTPClient发送HttpGet和HttpPost请求,去掉日志可以直接使用


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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


/**
 * @description 发送http请求工具类
 * @author ykzhao
 * @create 2020-2-25
 */
public class HttpRequestUtil {

	/**
	 * @description 发送post请求
	 * @author ykzhao
	 * @create 2020-2-25
	 * @param url:接口地址
	 * @param header请求头参数
	 * @param params接口请求参数
	 * @throws Exception
	 */
	public static Object sendPostRequest(String url, Map<String, String> header, Map<String, String> params)
			throws Exception {
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		try {
			client = HttpClients.createDefault();
			// 创建http请求
			HttpPost httpPost = new HttpPost(url);
			// 设置header参数
			for (Map.Entry<String, String> entry : header.entrySet()) {
				httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
			}
			// 设置请求参数
			httpPost.setEntity(new UrlEncodedFormEntity(createParam(params), Consts.UTF_8));
			// 发送请求
			response = client.execute(httpPost);
			// 获取返回结果
			HttpEntity entity = response.getEntity();
			String str = EntityUtils.toString(entity, "UTF-8");
			return str;
		} catch (Exception e) {
			throw e;
		} finally {
			// 关闭资源
			if (response != null) {
				try {
					response.close();
				} catch (Exception e) {
					_log.error("关闭response异常:" + e);
					throw e;
				}
			}
			if (client != null) {
				try {
					client.close();
				} catch (Exception e) {
					_log.error("关闭client异常:" + e);
					throw e;
				}
			}
		}
	}

	/**
	 * @description 发送post请求
	 * @author ykzhao
	 * @create 2020-2-25
	 * @param url:接口地址
	 * @param header请求头参数
	 * @param params接口请求参数
	 * @throws Exception
	 */
	public static Object sendGetRequest(String url, Map<String, String> header) throws Exception {
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		try {
			client = HttpClients.createDefault();
			URIBuilder uriBuilder = new URIBuilder(url);
			HttpGet httpGet = new HttpGet(uriBuilder.build());
			// 设置header参数
			for (Map.Entry<String, String> entry : header.entrySet()) {
				httpGet.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
			}
			response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString(entity, "UTF-8");
			return result;

		} catch (Exception e) {
			throw e;
		} finally {
			// 关闭资源
			if (response != null) {
				try {
					response.close();
				} catch (Exception e) {
					_log.error("关闭response异常:" + e);
					throw e;
				}
			}
			if (client != null) {
				try {
					client.close();
				} catch (Exception e) {
					_log.error("关闭client异常:" + e);
					throw e;
				}
			}
		}
	}

	/**
	 * @description BasicNameValuePair通常是用来封装post请求中的参数名称和值
	 * @author ykzhao
	 * @create 2020-2-25
	 */
	private static List<NameValuePair> createParam(Map<String, String> params) {
		// 建立一个NameValuePair数组,用于存储欲传送的参数
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		if (params != null) {
			for (String k : params.keySet()) {
				nvps.add(new BasicNameValuePair(k, params.get(k).toString()));
			}
		}
		return nvps;
	}
}

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值