https://square.github.io/okhttp/是一个很好的http交互库。然后,如果需要,您可以使用 Jackson/Gson 解析对类型化对象的响应
用法
Run Code Online (Sandbox Code Playgroud)public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = client.newCall(request).execute(); return response.body().string(); }
行家:
Run Code Online (Sandbox Code Playgroud)<dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.4.0</version> </dependency>
或摇篮:
实施 'com.squareup.retrofit2:retrofit:2.4.0'
如果你想用巴顿坦克杀死一只蚂蚁,你有各种其他选择,例如 spring、netflix OSS - feign 客户端 + Ribbon 等
话虽这么说,首先,我会尝试一下 Kayaman 发布的内容,https://www.baeldung.com/java-9-http-client作为它的一个较少的依赖项。
| 归档时间: |
|
| 查看次数: |
4351 次 |
| 最近记录: |