Http请求接口HttpClient

最近在开发rest接口,后端有大量的http请求,找到了一个非常实用的http请求工具 HttpClient,如果并发量高还是升级到高版本,我用的4.0.1

pom.xml


	4.0.0

	com.test.m2
	m2-t1
	0.0.1-SNAPSHOT
	jar

	m2-t1
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			4.7
			test
		

		
			org.apache.httpcomponents
			httpclient
			4.0.1
		

	


HttpUtil.java

package com.test.m2.t1;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
	private static HttpClient client = new DefaultHttpClient();

	/**
	 * get请求
	 * 
	 * @param url
	 * @param map
	 * @return
	 */
	public static String httpGet(String url, Map map) {
		List params = getparamsbyMap(map);
		String body = null;

		try {
			HttpGet get = new HttpGet(url);
			String urlParam = EntityUtils.toString(new UrlEncodedFormEntity(
					params));
			get.setURI(new URI(get.getURI().toString() + "?" + urlParam));
			HttpResponse resp = client.execute(get);
			HttpEntity entity = resp.getEntity();
			body = EntityUtils.toString(entity);
			if (entity != null) {
				entity.consumeContent(); // 关闭底层流资源,如果高版本的httpclient,该方法废弃,使用EntityUtils.consume(entity)进行关闭;
			}

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		return body;
	}
	
	/**
	 * 可以设置超时时间的get请求
	 * @param url
	 * @param map
	 * @param connectionTimeOut 毫秒
	 * @param readTimeOut 毫秒
	 * @return
	 */
	public static String httpGet(String url, Map map,Integer connectionTimeOut,Integer readTimeOut){
		setTimeOut(connectionTimeOut, readTimeOut);
		return httpGet(url, map);
	}
	
	/**
	 * post请求
	 * 
	 * @param url
	 * @param map
	 * @return
	 */
	public static String httpPost(String url, Map map) {
		List params = getparamsbyMap(map);
		String body = null;
		try {
			HttpPost post = new HttpPost(url);
			post.setEntity(new UrlEncodedFormEntity(params));
			HttpResponse resp = client.execute(post);
			HttpEntity entity = resp.getEntity();
			body = EntityUtils.toString(entity);
			if (entity != null) {
				entity.consumeContent(); // 关闭底层流资源,如果高版本的httpclient,该方法废弃,使用EntityUtils.consume(entity)进行关闭;
			}

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return body;
	}
	
	/**
	 * 可设置超时时间的post请求
	 * @param url
	 * @param map
	 * @param connectionTimeOut 毫秒
	 * @param readTimeOut 毫秒
	 * @return
	 */
	public static String httpPost(String url, Map map,Integer connectionTimeOut,Integer readTimeOut){
		setTimeOut(connectionTimeOut, readTimeOut);
		return httpPost(url, map);
	}
	
	/**
	 * 格式化参数
	 * @param map
	 * @return
	 */
	private static List getparamsbyMap(Map map) {
		List params = new ArrayList();

		if (map == null || map.isEmpty()) {
			return params;
		}

		Set keys = map.keySet();
		for (String key : keys) {
			params.add(new BasicNameValuePair(key, map.get(key).toString()));
		}

		return params;
	}

	/**
	 * 设置超时时间
	 * @param connectionTimeOut 毫秒
	 * @param readTimeout 毫秒
	 */
	private static void setTimeOut(Integer connectionTimeOut,Integer readTimeout){
		client.getParams().setParameter("http.connection.timeout", connectionTimeOut);
		client.getParams().setParameter("http.socket.timeout", readTimeout);
	} 
	
	
}


测试类:

package com.test.m2.t1;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class HttpUtilTest {
	
	@Test
	public void testGet(){
		String url="http://****:8060/abc/a/b";
		Map param = new HashMap();
		param.put("a", "SN1002");
		param.put("b", "李四");
		String data = HttpUtil.httpGet(url, param,1000,1000);
		System.out.println(data);
	}
	
	@Test
	public void testPost(){
		String url="http://****:8060/abc/a/b";
		Map param = new HashMap();
		param.put("a", "SN1002");
		param.put("b", "李四");
		String data = HttpUtil.httpGet(url, param,1000,1000);
		System.out.println(data);
	}
}


httpUtil需要注意的是请求后,entity.consumeContent() 进行关闭资源;

你可能感兴趣的:(javase)