HttpClient 使用指南 - POST篇

本文详细介绍如何使用HttpClient进行POST请求,包括基本的表单提交、JSON提交及上传附件等多种场景,并提供示例代码。

一,概述

由于目前大部分项目都基于接口调用,jwt安全机制以及分布式, 使得 HttpClient/RestTemplate 在项目中使用得非常之多, 接下来我将简单地介绍 HttpClient 4 API 的使用。不废话, 直接上码。


二,基本的 POST 用法

2.1 FORM 表单提交方式

基本的表单提交, 像注册,填写用户信息等 ... 都是一些基本的用法

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://api.example.com/");
 
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "Adam_DENG"));
params.add(new BasicNameValuePair("password", "password"));
httpPost.setEntity(new UrlEncodedFormEntity(params));
 
CloseableHttpResponse response = client.execute(httpPost);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();

2.2 JSON 提交方式

JSON的提交方式, 基本都是行业标准了。

RequestModel model = new RequestModel();
model.setRealname("Adam_DENG");
model.setPassword("password");

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(targetUrl);
// 这里使用了对象转为json string
String json = JSON.toJSONString(model);
StringEntity entity = new StringEntity(json, "UTF-8");
// NOTE:防止中文乱码
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json; charset=UTF-8");
httpPost.setHeader("Content-type", "application/json; charset=UTF-8");
CloseableHttpResponse response = client.execute(httpPost);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();

三, 高级 POST用法

3.1 上传附件

由于上传附件方式 服务端基本上都是 MultipartFile 模式。 所以客户端也是相对于的 MultipartEntityBuilder

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(targetUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 这里支持 `File`, 和 `InputStream` 格式, 对你来说哪个方便, 使用哪个。
// 因为我的文件是从 `URL` 拿的,所以代码如下, 其他形式类似

InputStream is = new URL("http://api.demo.com/file/test.txt").openStream();
builder.addBinaryBody("file", is, ContentType.APPLICATION_OCTET_STREAM, "test.txt");

HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpResponse response = client.execute(httpPost);

3.2 上传附件的同时传递 Url 参数

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(targetUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();

InputStream is = new URL("http://api.demo.com/file/test.txt").openStream();
builder.addBinaryBody("file", is, ContentType.APPLICATION_OCTET_STREAM, "test.txt");

StringBody realnameBody = new StringBody("Adam_DENG", ContentType.create("text/plain", Charset.forName("UTF-8")));
builder.addPart("realname", realnameBody);

HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpResponse response = client.execute(httpPost);

四, 结论

这个教程中我只是介绍了我们平时在项目中使用最多的几个 HttpClient API 方法。

转载于:https://my.oschina.net/u/2986558/blog/827410

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值