开始之前的准备工作:
1、建立测试响应数据所需的服务项目ServerProject(实际开发中是请求网络服务器),提供服务,并启动
服务的的controller代码:
@RequestMapping("/query")
public void query(@RequestParam("name")String username) throws Exception {
//doing business......
resp.getWriter().println("Welcome "+username+"!,"+"start doing Service ......ing");
}
2、创建一个项目ClientProject用于测试请求
开始使用
第一步: ClientProject添加HttpComponents的Maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<!-- StringEntity设置传输参数时,使用fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
参考地址:添加Maven依赖官方参考地址
httpclient : 官方文档
几个相关项目(具体使用文档参考上面的官方文档连接):
HttpClient Cache : 提供了缓存的api
HttpMime :文件上传下载操作api
Fluent HC :官方基于流式,封装了httpclient的api,简化操作
第二步: 编写测试代码
public class HttpClientTest {
public static void main(String[] args) throws Exception{
//start get
//1、创建客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
//2、创建Http的请求方式get,post etc... 需要传入请求地址
HttpGet httpGet = new HttpGet("http://localhost:8888/study/test/query?name=liban");
//3、httpClient执行请求,并获取返回结果
CloseableHttpResponse response = httpClient.execute(httpGet);
//4、输出获取到的结果
//从response中取出返回的entity
HttpEntity entity = response.getEntity();
//输出
System.out.println("get返回状态行:"+response.getStatusLine());
System.out.println("get返回结果:"+EntityUtils.toString(entity));
//5、关闭连接 也可以直接调用close()方法
HttpClientUtils.closeQuietly(response);
//end get
//start post
//1、创建post请求
HttpPost httpPost = new HttpPost("http://localhost:8888/study/test/query");
//2、设置请求参数HttpEntity
//UrlEncodedFormEntity的方式设置请求参数,这是假的post请求,参数是拼接再url中的如:?name=myName
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("name","myName"));
UrlEncodedFormEntity HttpEntity = new UrlEncodedFormEntity(pairs);
httpPost.setEntity(HttpEntity);
//3、发送请求
CloseableHttpResponse httpPostResponseUrl = httpClient.execute(httpPost);
//4、获取返回结果
String postResultUrl = EntityUtils.toString(httpPostResponseUrl.getEntity());
//5、输出结果
System.out.println("UrlEncodedFormEntity返回状态行:"+httpPostResponseUrl.getStatusLine());
System.out.println("UrlEncodedFormEntity返回结果:"+postResultUrl);
//StringEntity的方式,这种方式比较自由,可以传输自定义的数据格式,只需要后台能处理即可
//这种方式是真正的post请求,请求参数是防止body中,后台通过流的方式获取数据
//JSONObject使用阿里巴巴的fastjson
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","StringEntity");
//创建StringEntity,并设置ContentType
StringEntity stringEntity = new StringEntity(jsonObject.toString(),ContentType.APPLICATION_JSON);
//stringEntity加入http中
httpPost.setEntity(stringEntity);
//3、发送http请求
CloseableHttpResponse httpPostResponse = httpClient.execute(httpPost);
//4、获取返回结果
String postResult = EntityUtils.toString(httpPostResponse.getEntity());
//5、输出结果
System.out.println("StringEntity返回状态行:"+httpPostResponse.getStatusLine());
System.out.println("StringEntity返回结果:"+postResult);
//6、关闭
httpPostResponse.close();
//end post
//关闭client
HttpClientUtils.closeQuietly(httpClient);
}
}
输出结果如下:
get返回状态行:HTTP/1.1 200 OK
get返回结果:Welcome liban!,start doing Service ......ing
UrlEncodedFormEntity返回状态行:HTTP/1.1 200 OK
UrlEncodedFormEntity返回结果:Welcome myName!,start doing Service ......ing
StringEntity返回状态行:HTTP/1.1 200 OK
StringEntity返回结果:Welcome StringEntity!,start doing Service ......ing