文末附有包装类,复制即用
使用方法如下使用UniRest发送POST、GET、PUT、DELETE请求非常方便,还可以用各种格式的请求体发送请求,比如params格式、body的raw、form-data格式等等。
首先引入maven包,作为依赖包。
Http请求 1. 发送POST请求com.konghq unirest-java 3.0.00 com.alibaba fastjson 1.2.49
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, Map2. 发送GET请求map) { HttpResponse jsonResponse = null; try { jsonResponse = Unirest.post(url) .fields(map) .asString(); } catch (UnirestException e) { e.printStackTrace(); } return JSONObject.parseObject(jsonResponse.getBody().toString()); }
无参
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,Map3. PUT请求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()); }
无参数(或者参数拼接在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)
.header("content-type", "application/json")
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
Https请求
https请求只需要在这个类里添加一个静态代码块即可
static {
Unirest.config().verifySsl(false);
Unirest.config().connectTimeout(30000);
}
ps:关于cookie的处理,本人提供一种方法,仅供参考
很多业务场景都会涉及到登陆这个操作,并且大多数接口是基于登陆后拿到的cookie来进行鉴权的。如果没这个cookie,请求会被拒绝,返回402状态码。
我们在这个类里加入一个静态变量,就叫他cookie
static String cookie = "";
假设你的登陆是post请求
你就加一个判断
public JSONObject doPost(String url, String json) {
HttpResponse jsonResponse = null;
try {
JsonNode jsonNode = new JsonNode(json);
jsonResponse = Unirest.post(url)
.body(jsonNode)
.header("cookie", cookie)//在每个请求方法里都加上这个,即使不需要cookie的请求也不会出错,加就完了
.header("content-type", "application/json")
.asJson();
//这里面的逻辑就需要你去解析返回来的cookie格式了
if (cookie.equals("")) {
Headers headers = jsonResponse.getHeaders();
List h = headers.get("Set-cookie");
for (String s1 : h
) {
String[] ss = s1.split(";");
for (String s2 : ss
) {
if (s2.contains("cookie")) {
cookie = s2;
break;
}
}
}
}
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
一些不常用的我就没写在这里,谢谢,希望对你有所帮助
附上整体代码,复制粘贴即用
import com.alibaba.fastjson.JSONObject;
import kong.unirest.*;
import java.util.List;
import java.util.Map;
public class HttpUtil {
static {
Unirest.config().verifySsl(false);
Unirest.config().connectTimeout(30000);
}
static String cookie = "";
public JSONObject doGet(String url) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.header("cookie", cookie)
.asJson();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doGet(String url, Map map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.get(url)
.header("content-type", "application/json")
.header("cookie", cookie)
.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")
.header("cookie", cookie)
.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")
.header("cookie", cookie)
.asJson();
} 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")
.header("cookie", cookie)
.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);
jsonResponse = Unirest.post(url)
.body(jsonNode)
.header("cookie", cookie)
.header("content-type", "application/json")
.asJson();
//TODO 这里cookie的获取逻辑需要自己解析来写
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPostBytxt(String url, String txt) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.body(txt)
.header("cookie", cookie)
.header("content-type", "application/json")
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
public JSONObject doPostByForm(String url, Map map) {
HttpResponse jsonResponse = null;
try {
jsonResponse = Unirest.post(url)
.fields(map)
.header("cookie", cookie)
.asString();
} catch (UnirestException e) {
e.printStackTrace();
}
return JSONObject.parseObject(jsonResponse.getBody().toString());
}
}



