日常工作难免遇到调用其他项目接口,一般都是restful风格,这个并非强制风格,根据项目不同能使用的工具不同,主要受限于项目类型:maven项目,一般项目等。有原生http协议进行交互,java原生自带对http的支持并不好用,或者是httpclient,我们不得不重复造轮子。下边就介绍几种通用常用好用的方法。
1. 第一种http-requst
最大的特点是基于URLConnection实现,不依赖HttpClien
maven引入
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>5.6</version>
</dependency>
Get请求请求示例
public static HttpRequest get(final URL url) throws HttpRequestException {
return new HttpRequest(url, METHOD_GET);
}
Get请求获取响应报文
String response = HttpRequest.get("http://www.baidu.com").body();
System.out.println("Response was: "+response);
Get请求,获取响应码
int code = HttpRequest.get("http://google.com").code();
Get请求加请求参数:可以直接加在get方法里,选择是否进行编码,也可以用Map传参
-第一种写法
HttpRequest request = HttpRequest.get("http://google.com", true, 'macId', "10051", "size", "测试机构");
-第二种
写法2: Map data = new HashMap();
data.put("macId", "10051");
data.put("macName", "测试机构");
String result =HttpRequest.get("http://google.com")form(data).body();
System.out.println("result:" + resp);
- post请求请求示例
发一个带文件的POST请求
HttpRequest request = HttpRequest.post("url”);
request.header("Content-Type", "multipart/form-data;boundary=AaB03x");
request.part("imagefile", "test.log", "image/jpeg", new File("d:/test/test.jpg"));
发一个带Form的POST
Map<String, String> data = new HashMap<String, String>();
data.put("user", "A User");
data.put(“state”, “CA”);
HttpRequest request = HttpRequest.post(url).form(data);
发送带JSON的POST
JsonObject jsonContent = new JsonObject();
jsonContent.addProperty("content",msgBody);
JsonObject jsonData = new JsonObject();
jsonData.add("data",jsonContent);
jsonData.addProperty("subtype",subType);
HttpRequest httpRequest = HttpRequest.post(url).acceptJson();
httpRequest.send(jsonData.toString());
int code = httpRequest.code();
String body = httpRequest.body();
发送请求上传附件
HttpRequest request=HttpRequest.post("http://google.com");
request.part("status[body]","Making a multipart request");
request.part("status[image]",newFile("/home/kevin/Pictures/ide.png"));
if(request.ok()){
System.out.println("Status was updated");
}
常用http请求配置
HttpRequest request = HttpRequest.get("https://google.com");
//信任所有证书
request.trustAllCerts();
//信任所有地址
request.trustAllHosts();
//设置请求超时时间
request.connectTimeout(60000);
//设置读取超时时间
request.readTimeout(60000);
以上设置支持Builder模式
String resp = HttpRequest.post("http://www.baidu.com").trustAllCerts().trustAllHosts()
.form(data)
.connectTimeout(60000)
.readTimeout(60000)
.body();复制代码
配置http代理
HttpRequest request = HttpRequest.get("https://google.com");
//Configure proxy
request.useProxy("localhost", 8080);
//Optional proxy basic authentication
request.proxyBasic("username", "p4ssw0rd");
2.hutool
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的
maven引入
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.19</version>
</dependency>
针对最为常用的GET和POST请求,HttpUtil封装了两个方法,
HttpUtil.get
HttpUtil.post
这两个方法用于请求普通页面,然后返回页面内容的字符串,同时提供一些重载方法用于指定请求参数(指定参数支持File对象,可实现文件上传,当然仅仅针对POST请求)。
GET请求例子:
// 最简单的HTTP请求,可以自动通过header等信息判断编码,不区分HTTP和HTTPS
String result1= HttpUtil.get("https://www.baidu.com");
// 当无法识别页面编码的时候,可以自定义请求页面的编码
String result2= HttpUtil.get("https://www.baidu.com", CharsetUtil.CHARSET_UTF_8);
//可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("city", "北京");
String result3= HttpUtil.get("https://www.baidu.com", paramMap);
POST请求例子:
HashMap<String, Object> paramMap = new HashMap<>()