Unirest 是一个轻量级的 HTTP 请求库,涵盖 Node、Ruby、Java、PHP、Python、Objective-C、.NET 等多种语言。可发起 GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS 请求。
Java 版本的示例代码:
HttpResponseJSONResponse = Unirest.post("http://httpbin.org/post") .header("accept", "application/json") .fIEld("parameter", "value") .field("foo", "bar") .asJson(); //参数处理 Unirest.get("http://httpbin.org/{method}") .routeParam("method", "get") .field("name", "Mark") .asJson(); //异步请求 Future > future = Unirest.post("http://httpbin.org/post") .header("accept", "application/json") .field("param1", "value1") .field("param2", "value2") .asJsonAsync(new Callback () { public void failed(UnirestException e) { System.out.println("The request has failed"); } public void completed(HttpResponse response) { int CODE = response.getCode(); Map headers = response.getHeaders(); JsonNode body = response.getBody(); InputStream rawBody = response.getRawBody(); } public void cancelled() { System.out.println("The request has been cancelled"); } });