Apache中怎么实现一个httpclient封装类,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
基于apache httpcomponents组件,首先依赖jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
先来看使用方法:
/**
* @author tanghc
*/
public class HttpTest {
/**
* 普通get请求
* @throws IOException
*/
@Test
public void get() throws IOException {
String s = HttpHelper.get("http://localhost:8089/http/get")
// 添加请求参数
.parameter("id", "1")
.parameter("name", "jim")
.execute()
.asString();
System.out.println(s);
}
/**
* post提交表单
* @throws IOException
*/
@Test
public void postForm() throws IOException {
Map<String, Object> form = new HashMap<>();
form.put("id", 1);
form.put("name", "tom");
String s = HttpHelper.postForm("http://localhost:8089/http/postForm", form)
.execute()
.asString();
System.out.println(s);
}
/**
* post提交表单,并上传文件。multipart
* @throws IOException
*/
@Test
public void postFormFile() throws IOException {
Map<String, Object> form = new HashMap<>();
HttpHelper.UploadFile file = new HttpHelper.UploadFile("file", "a.txt", "hello world".getBytes());
form.put("id", 1);
form.put("name", "tom");
String s = HttpHelper.postForm("http://localhost:8089/http/postFormFile", form, file)
.execute()
.asString();
System.out.println(s);
}
/**
* post提交json
* @throws IOException
*/
@Test
public void postJson() throws IOException {
Map<String, Object> form = new HashMap<>();
form.put("id", 1);
form.put("name", "tom");
String json = JSON.toJSONString(form);
String s = HttpHelper.postJson("http://localhost:8089/http/postJson", json)
.execute()
.asString();
System.out.println(s);
}
/**
* 设置header
* @throws IOException
*/
@Test
public void setHeader() throws IOException {
String s = HttpHelper.get("http://localhost:8089/http/header")
// 设置单个header
.header("token", "xxx")
.execute()
.asString();
System.out.println(s);
Map<String, String> headers = new HashMap<>();
headers.put("token", "bbbb");
String s2 = HttpHelper.get("http://localhost:8089/http/header")
// 设置全部header
.headers(headers)
.execute()
.asString();
System.out.println(s2);
}
/**
* 返回流
* @throws IOException
*/
@Test
public void getStream() throws IOException {
InputStream inputStream = HttpHelper.get("http://localhost:8089/http/stream")
.execute()
.asStream();
String s = IOUtils.toString(inputStream);
System.out.println(s);
}
/**
* 获取http状态
* @throws IOException
*/
@Test
public void getStatus() throws IOException {
HttpHelper.ResponseResult responseResult = HttpHelper.get("http://localhost:8089/http/get")
.parameter("id", "1")
.parameter("name", "jim")
.execute();
int status = responseResult.getStatus();
if (status == HttpStatus.SC_OK) {
System.out.println(responseResult.asString());
}
}
/**
* 获取响应头
* @throws IOException
*/
@Test
public void getHeaders() throws IOException {
Map<String, Object> form = new HashMap<>();
form.put("id", 1);
form.put("name", "tom");
String json = JSON.toJSONString(form);
HttpHelper.ResponseResult responseResult = HttpHelper.postJson("http://localhost:8089/http/responseHeader", json)
.execute();
Map<String, String> headers = responseResult.getHeaders();
System.out.println(headers.get("code"));
System.out.println(headers.get("token"));
System.out.println(responseResult.asString());
}
/**
* 获取HttpResponse对象
* @throws IOException
*/
@Test
public void getResponse() throws IOException {
HttpHelper.ResponseResult responseResult = HttpHelper.get("http://localhost:8089/http/get")
.parameter("id", "1")
.parameter("name", "jim")
.execute();
HttpResponse response = responseResult.getResponse();
Locale locale = response.getLocale();
System.out.println(locale);
}
/**
* 自定义
* @throws IOException
*/
@Test
public void custom() throws IOException {
String s = HttpHelper.create()
.url("http://localhost:8089/http/get")
.method("get")
.parameter("id", "1")
.parameter("name", "jim")
.header("token", "xx")
.execute()
.asString();
System.out.println(s);
String json = "{ \"name\": \"Jim\" }";
String s2 = HttpHelper.create()
.url("http://localhost:8089/http/postJson")
.method("POST")
.header("token", "xx")
.entity(new StringEntity(json, ContentType.APPLICATION_JSON))
.execute()
.asString();
System.out.println(s2);
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3658366/blog/5025148