代码调用外部HTTP请求接口

转载链接样例:https://www.cnblogs.com/guxiong/p/6661272.html
https://zhidao.baidu.com/question/627561819246516844.html

package net.ninehkj.logistics.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
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;

/**
 * 	调用HTTP接口返回数据
 * @author Administrator
 *
 */
public class InterfaceUtils {

	@Resource
	private CloseableHttpClient  httpClient;
	/**
	 * Post请求(此方法会返回字符串)
	 * @param payUrl	路径
	 * @param param		请求数据
	 * @return
	 * @throws UnsupportedEncodingException 
	 * 	将map集合转换为json字符串
	 * JSONObject jsonObject = JSONObject.fromObject(map);
	 */
	public static String JSONPostHttpCallOtherInterfaceUtils(String payUrl, String json) throws UnsupportedEncodingException {
    	
		/**
    	 * 	创建Httpclient对象
    	 */
		CloseableHttpClient  httpClient = HttpClients.createDefault();
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String result = "";
		try {
			/**
			 * 	创建HTTP请求并放入请求地址
			 */
			HttpPost httpPost = new HttpPost(payUrl);
			if(!"".equals(json)) {
				/**
				 * 	将数据放入UrlEncodedFormEntity会自动将数据编码为合适内容
				 */
				StringEntity requestEntity = new StringEntity(json, "utf-8");
				requestEntity.setContentEncoding("UTF-8");
				/**
				 * 给httpPost设置JSON格式的参数
				 */
				httpPost.setHeader("Content-type", "application/json");
				httpPost.setEntity(requestEntity);
				/**
				 * 发送HTTP请求获取返回值
				 */
				result = httpClient.execute(httpPost, responseHandler);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return result;
	}
	
	
	
	
	/**
	 * Post请求
	 * @param payUrl	路径
	 * @param param		请求数据
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String PostHttpCallOtherInterfaceUtils(String payUrl, Map<String, String> param) throws UnsupportedEncodingException {
    	
		/**
    	 * 	创建Httpclient对象
    	 */
		CloseableHttpClient  httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String result = "";
		try {
			/**
			 * 	创建HTTP请求并放入请求地址
			 */
			HttpPost httpPost = new HttpPost(payUrl);
			if(null != param) {
				/**
				 * 	此集合用于存储传送的数据
				 */
				List<NameValuePair> list = new LinkedList<NameValuePair>();
				for (String key : param.keySet()) {
					/**
					 * 	遍历map中的参数放入集合中
					 * 	BasicNameValuePair对象中传入参数该map的key和当前key对应的value值
					 * 	为什么使用BasicNameValuePair,因为我们在下面使用UrlEncodedFormEntity
					 * 	对BasicNameValuePair数据进行编码时只能接受List<? extends NameValuePair>
					 * 	为参数。链接:https://blog.csdn.net/zdb292034/article/details/80663792
					 * 	
					 */
					list.add(new BasicNameValuePair(key, param.get(key)));
				}
				/**
				 * 	将数据放入UrlEncodedFormEntity会自动将数据编码为合适内容:param1=value1¶m2=value2
				 * 	如果内容不规范,可以使用StringEntity
				 */
				UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list);
				httpPost.setEntity(urlEncodedFormEntity);
				/**
				 * 	进行HTTP请求
				 */
				response = httpClient.execute(httpPost);
//				httpClient
				/**
				 * 	执行http请求
				 */
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(null != response) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42563880/article/details/88534370