HttpClient 工具类

pom.xml加入jar包
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.2</version>
</dependency>
package com.jk.songxj.utils;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.http.Header;
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.entity.StringEntity;
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 com.alibaba.fastjson.JSON;

/**
 * 
 * 类: HttpClient <br>
 * 描述: httpclient工具类 <br>
 * 作者: song<br>
 * 时间: 2017年7月21日 下午3:15:27
 */
public class HttpClientUtil {
	
	static CloseableHttpClient client = null;
	static {
		client = HttpClients.createDefault();
	}
	
	/**
	 * 
	 * 方法: get <br>
	 * 描述: get请求 <br>
	 * 作者: song<br>
	 * 时间: 2017年7月21日 下午3:15:25
	 * @param url
	 * @param params
	 * @return
	 * @throws Exceptionm
	 */
	public static String get(String url,HashMap<String, Object> params) throws Exception {
		HttpGet httpGet = new HttpGet();
		Set<String> keySet = params.keySet();
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(url).append("?t=").append(System.currentTimeMillis());
		for (String key : keySet) {
			stringBuffer.append("&").append(key).append("=").append(params.get(key));
		}
		httpGet.setURI(new URI(stringBuffer.toString()));
		CloseableHttpResponse execute = client.execute(httpGet);
		int statusCode = execute.getStatusLine().getStatusCode();
		if (200 != statusCode) {
			return "";
		}
		return EntityUtils.toString(execute.getEntity(), "utf-8");
	}
	
	/**
	 * 
	 * 方法: get2 <br>
	 * 描述: 斜杠传参 <br>
	 * 时间: 2019年7月10日 下午3:29:01
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String get2(String url,String params) throws Exception {
		HttpGet httpGet = new HttpGet();
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(url).append("/").append(params);
		httpGet.setURI(new URI(stringBuffer.toString()));
		CloseableHttpResponse execute = client.execute(httpGet);
		int statusCode = execute.getStatusLine().getStatusCode();
		if (200 != statusCode) {
			return "";
		}
		return EntityUtils.toString(execute.getEntity(), "utf-8");
	}
	/**
	 * 
	 * 方法: post <br>
	 * 描述: post请求 <br>
	 * 作者: song<br>
	 * 时间: 2017年7月21日 下午3:20:31
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String post(String url,HashMap<String, Object> params) throws Exception {
		HttpPost httpPost = new HttpPost();
		httpPost.setURI(new URI(url));
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			NameValuePair e = new BasicNameValuePair(key, params.get(key).toString());
			parameters.add(e);
		}
		HttpEntity entity = new UrlEncodedFormEntity(parameters , "utf-8");
		httpPost.setEntity(entity );
		CloseableHttpResponse execute = client.execute(httpPost);
		int statusCode = execute.getStatusLine().getStatusCode();
		if (200 != statusCode) {
			return "";
		}
		return EntityUtils.toString(execute.getEntity(), "utf-8");
	}
	/**
	 * 
	 * 方法: post <br>
	 * 时间: 2019年7月10日 下午3:29:21
	 * @param url
	 * @param params
	 * @param headers
	 * @return
	 * @throws Exception
	 */
	public static String post(String url,HashMap<String, Object> params,HashMap<String, Object> headers) throws Exception {
		HttpPost httpPost = new HttpPost();
		Set<String> keySet2 = headers.keySet();
		Iterator<String> iterator = keySet2.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			String value = headers.get(key).toString();
			httpPost.addHeader(key, value);
		}
	
		httpPost.setURI(new URI(url));
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			NameValuePair e = new BasicNameValuePair(key, params.get(key).toString());
			parameters.add(e);
		}
		HttpEntity entity = new UrlEncodedFormEntity(parameters , "utf-8");
		httpPost.setEntity(entity );
		CloseableHttpResponse execute = client.execute(httpPost);
		int statusCode = execute.getStatusLine().getStatusCode();
		if (200 != statusCode) {
			return "";
		}
		return EntityUtils.toString(execute.getEntity(), "utf-8");
	}
	
	/**
	 * 请求参数为json字符串
	 * @param url
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String postJson(String url,HashMap<String, Object> params) throws Exception {
		HttpPost httpPost = new HttpPost();
		httpPost.setURI(new URI(url));
		String jsonString = JSON.toJSONString(params);
		StringEntity stringEntity = new StringEntity(jsonString,"utf-8");
		stringEntity.setContentEncoding("UTF-8");
		stringEntity.setContentType("application/json");//发送json数据需要设置contentType
		httpPost.setEntity(stringEntity);
		CloseableHttpResponse execute = client.execute(httpPost);
		int statusCode = execute.getStatusLine().getStatusCode();
		if (200 != statusCode) {
			System.out.println(execute.getEntity());
			return "";
		}
		return EntityUtils.toString(execute.getEntity(), "utf-8");
	}
	/**
	 * postJson使用示例代码
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String url = "http://localhost:8081/express/findProductList.do";
		HashMap<String, Object> params = new HashMap<String, Object>();
		HashMap<String, Object> searchinfo = new HashMap<String, Object>();
		searchinfo.put("productType", "手机测试");
		params.put("searchinfo", searchinfo);
		params.put("page", 2);
		
		String postJson = HttpClientUtil.postJson(url , params );
		System.out.println(postJson);
	}
}

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值