文末附有包装类,复制即用
使用UniRest发送POST、GET、PUT、DELETE请求非常方便,还可以用各种格式的请求体发送请求,比如params格式、body的raw、form-data格式等等。
使用方法如下
首先引入maven包,作为依赖包。
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.0.00</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
Http请求
1. 发送POST请求
raw格式(json)
public JSONObject doPost(String url, String json) {
HttpResponse jsonResponse = null;
try {
JsonNode jsonNode = new JsonNode(json);
jsonResponse = Unirest.post(url)
.body(jsonNode)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
raw格式(string)
public JSONObject doPostBytxt(String url,String txt) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.body(txt)
.header("content-type", "application/json")
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
params格式(map 上传文件可以用这个)
public JSONObject doPostByParams(String url, Map<String ,Object> map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.fields(map)
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
2. 发送GET请求
无参
public JSONObject doGet(String url) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
params格式参数(map)
public JSONObject doGet(String url,Map<String,Object> map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.queryString(map)
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
3. PUT请求
无参数(或者参数拼接在url后面的)
public JSONObject doPut(String url) {
kong.unirest.HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.put(url)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
raw格式(json)
public JSONObject doPut(String url, String json) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.put(url)
.body(json)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
4.DELETE
无参数
public JSONObject doDelete(String url) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.delete(url)
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
Https请求
https请求只需要在这个类里添加一个静态代码块即可
static {
Unirest.config().verifySsl(false);
}
一些不常用的我就没写在这里,谢谢,希望对你有所帮助
附上整体代码,复制粘贴即用
package com.oushu.utils.util;
import com.alibaba.fastjson.JSONObject;
import kong.unirest.*;
import java.util.Map;
public class HttpUtil {
static {
Unirest.config().enableCookieManagement(true);
Unirest.config().verifySsl(false);
Unirest.config().connectTimeout(30000);
Unirest.config().automaticRetries(true);
}
static HttpUtil instance;
public static HttpUtil getInstance() {
if (instance == null) {
instance = new HttpUtil();
}
return instance;
}
public JSONObject doGet(String url) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doGet(String url, Map<String, Object> map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.queryString(map)
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPut(String url) {
kong.unirest.HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.put(url)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPut(String url, String json) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.put(url)
.body(json)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPutByForm(String url, Map<String, Object> map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.put(url)
.fields(map)
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doDelete(String url) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.delete(url)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doDelete(String url, String json) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.delete(url)
.body(json)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPost(String url, String json) {
HttpResponse jsonResponse = null;
try {
JsonNode jsonNode = new JsonNode(json);
while (jsonResponse == null) {
jsonResponse = Unirest.post(url)
.body(jsonNode)
.header("content-type", "application/json")
.asJson();
}
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public void doPostParallel(String url, String json) {
JsonNode jsonNode = new JsonNode(json);
try {
Unirest.post(url)
.body(jsonNode)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
}
public JSONObject doPostBytxt(String url, String txt) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.body(txt)
.header("content-type", "application/json")
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPostByForm(String url, Map<String, Object> map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.fields(map)
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
}