编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

轻量级的HTTP开发库 Unirest(巨量ip)

wxchong 2024-07-19 05:47:49 开源技术 15 ℃ 0 评论

Unirest 是一个轻量级的 HTTP 请求库,涵盖 Node、Ruby、Java、PHP、Python、Objective-C、.NET 等多种语言。可发起 GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS 请求。

Java 版本的示例代码:

HttpResponse<JsonNode> jsonResponse = 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<HttpResponse<JsonNode>> future = Unirest.post("http://httpbin.org/post")
 .header("accept", "application/json")
 .field("param1", "value1")
 .field("param2", "value2")
 .asJsonAsync(new Callback<JsonNode>() {
 public void failed(UnirestException e) {
 System.out.println("The request has failed");
 }
 public void completed(HttpResponse<JsonNode> response) {
 int code = response.getCode();
 Map<String, String> headers = response.getHeaders();
 JsonNode body = response.getBody();
 InputStream rawBody = response.getRawBody();
 }
 public void cancelled() {
 System.out.println("The request has been cancelled");
 }
});

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表