httpclient 工具类

20 篇文章 1 订阅
3 篇文章 0 订阅

Maven依赖

                 <dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.5.3</version>
		</dependency>

工具类

public class HttpUtils {
	
	private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000)
			.build();
	
	/***
	 *  Https
	 * @return 返回对象
	 */
	public static CloseableHttpClient createSSLClientDefault() {
		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				// 信任所有
				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
					return true;
				}
			}).build();
			HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
			return HttpClients.custom().setSSLSocketFactory(sslsf).build();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
		return HttpClients.createDefault();
}

	/**
	 * Get请求
	 * @param url 请求地址
	 * @param mapData 携带参数
	 * @return 返回Response 数据 
	 * @throws ClientProtocolException 发送的异常
	 * @throws IOException IO异常
	 * @throws URISyntaxException URI 异常
 	 */
	public HttpResponse doGet(String url, Map<String, String> mapData,Map<String, String> header)
			throws ClientProtocolException, IOException, URISyntaxException {

		CloseableHttpResponse response = null;
		HttpGet hg = new HttpGet();
		hg.setConfig(requestConfig);
		URIBuilder ub = new URIBuilder(url);
	
		//CloseableHttpClient client = HttpClients.createDefault();
	    	CloseableHttpClient client = HttpUtils.createSSLClientDefault();
		if (header!=null&&header.size()>0) {
			for (Map.Entry<String, String> e : header.entrySet()) {
			hg.setHeader(e.getKey(),e.getValue());
		
			}
		}
		if (mapData != null&&mapData.size()>0) {
			for (Map.Entry<String, String> e : mapData.entrySet()) {
				ub.addParameter(e.getKey(), e.getValue());
				
			}
		}
		hg.setURI(ub.build());
		response = client.execute(hg);
		return response;
	}
	
	
	public HttpResponse doPost(String url,Map<String, String> mapData) throws ClientProtocolException, IOException {
		CloseableHttpResponse response = null;
		HttpPost hp=new HttpPost(url);
		hp.setConfig(requestConfig);
		CloseableHttpClient client = HttpClients.createDefault();
		hp.setHeader("Content-type", "application/json; charset=utf-8");
		if (mapData!=null&&mapData.size()>0) {
			 String jsonData=  JSONObject.toJSONString(mapData);
			 StringEntity postEntity=new StringEntity(jsonData,"utf-8");
			 postEntity.setContentType("application/json");
			 hp.setEntity(postEntity);
		}
		
		response=client.execute(hp);
		return response;
	}

	
	public HttpResponse doPostHttps(String url,Map<String, String> mapData) throws ClientProtocolException, IOException {
		CloseableHttpResponse response = null;
		HttpPost hp=new HttpPost(url);
		hp.setConfig(requestConfig);
		CloseableHttpClient client = HttpUtils.createSSLClientDefault();
		hp.setHeader("Content-type", "application/json; charset=utf-8");
		if (mapData!=null&&mapData.size()>0) {
			 String jsonData=  JSONObject.toJSONString(mapData);
			 StringEntity postEntity=new StringEntity(jsonData,"utf-8");
			 postEntity.setContentType("application/json");
			 hp.setEntity(postEntity);
		}
		response=client.execute(hp);
		return response;
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值