官网
https://square.github.io/okhttp/
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
代码
package com.keeson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class OkHttpTest {
@Test
void TestGet() throws IOException {
OkHttpClient client = new OkHttpClient();
String url = "http://httpbin.org/get";
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
System.out.println(response);
if (response.isSuccessful()) {
System.out.println(response.body().string());
}
}
@Test
void TestPost() throws IOException {
OkHttpClient client = new OkHttpClient();
FormBody.Builder form = new FormBody.Builder();
form.add("name", "Tom");
form.add("age", "23");
Request request = new Request.Builder()
.url("http://httpbin.org/post")
.post(form.build())
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
@Test
void TestPostJson() throws IOException {
OkHttpClient client = new OkHttpClient();
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "Tom");
jsonObject.put("age", 23);
String data = JSON.toJSONString(jsonObject);
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data);
Request request = new Request.Builder()
.url("http://httpbin.org/post")
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
}
}
|