===========
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
≠=========
import okhttp3.*;
public class OkHttpPostExample {
public static void main(String[] args) {
String apiUrl = "https://api.example.com/data";
OkHttpClient client = new OkHttpClient();
String jsonBody = "{\"name\":\"John Doe\",\"age\":30}";
RequestBody body = RequestBody.create(
jsonBody, MediaType.get("application/json; charset=utf-8"));
Request request = new Request.Builder()
.url(apiUrl)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println("Response Code: " + response.code());
System.out.println("Response Body:");
System.out.println(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
}
}