HTTP POST 和 PUT 请求可以包含要提交的内容。只需要在创建 Request 对象时,通过 post 和 put 方法来指定要提交的内容即可。
HTTP POST 请求的基本示例:
public class PostString { public static void main(String[] args) throws IOException { OkHttpClient client = new OkHttpClient(); MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain"); String postBody = "Hello World"; Request request = new Request.Builder() .url("http:///3/image") .post(requestBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string());}