您的位置:首页 > 理论基础 > 计算机网络

OkHttpClient的使用方法

2018-03-22 14:31 531 查看

参考资料

https://github.com/square/okhttp/wiki

https://github.com/square/okhttp/wiki/Recipes

使用方法

get

// 获取OkHttpClient
private final OkHttpClient client = new OkHttpClient();
// 执行方法
public void run() throws Exception {
// 建立Builder
Request request = new Request.Builder()
.url("https://publicobject.com/helloworld.txt")
.build();
// 请求
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
// 获取头部
Headers responseHeaders = response.headers();
// 遍历头部信息
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
// 获取身体信息
System.out.println(response.body().string());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: