JavaEE或者Android的开发者对HttpClient一定不陌生,实际上这是一个在项目中经常被用到的框架,框架本身用法很简单,网上有很多这方面的资料,这里只是一个简单的使用总结。

  当在实际的项目开发中需要作为客户端发送HTTP请求时,HttpClient是个不错的选择,下面简单介绍两种GET请求。

方法一,直接拼接参数

package com.zws.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class ClientGet {
	static String uri = "http://localhost:8080/resteasy/rest/user/";
	static String CHAR_SET = "UTF-8";

	@Test
	public void get0() {
		String path = "getUserDetail2?userId=23&status=1";//直接拼接参数
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet get = new HttpGet(getUri(path));
		CloseableHttpResponse resp = null;
		try {
			resp = client.execute(get);
			int code = resp.getStatusLine().getStatusCode();//响应码
			System.out.println("resp code:" + code);
			HttpEntity entity = resp.getEntity();
			String msg = EntityUtils.toString(entity, CHAR_SET);//响应信息
			System.out.println("receive msg:" + msg);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (resp != null) {
				try {
					resp.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public String getUri(String path) {
		return uri + path;
	}
}

  方法二,利用URIBuilder辅助构造URI

package com.zws.httpclient;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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.util.EntityUtils;
import org.junit.Test;

public class ClientGet {
	static String uri = "http://localhost:8080/resteasy/rest/user/";
	static String CHAR_SET = "UTF-8";
	
	@Test
	public void get1() {
		String path = "/resteasy/rest/user/getUserDetail2";
		
		CloseableHttpClient client = HttpClients.createDefault();
		CloseableHttpResponse resp = null;
		try {
			URIBuilder url = new URIBuilder();
			url.setScheme("http");
			url.setHost("localhost:8080");
			url.setPath(path);
			url.setParameter("userId", "23");
			url.setParameter("status", "1");
			
			URI uri = url.build();	
			System.out.println("url:" + uri.toString());
			HttpGet get = new HttpGet(uri);
			
			resp = client.execute(get);
			int code = resp.getStatusLine().getStatusCode();
			System.out.println("resp code:" + code);
			
			HttpEntity entity = resp.getEntity();
			String msg = EntityUtils.toString(entity, CHAR_SET);
			System.out.println("receive msg:" + msg);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (resp != null) {
				try {
					resp.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}