OKHttp 3如何设置post请求编码?我用了RequestBody.create方法

图片描述

这是create的源码:

public static RequestBody create(MediaType contentType, String content) {

Charset charset = Util.UTF_8;

if (contentType != null) {

charset = contentType.charset();

if (charset == null) {

charset = Util.UTF_8;

contentType = MediaType.parse(contentType + "; charset=utf-8");

}

}

byte[] bytes = content.getBytes(charset);

return create(contentType, bytes);

}

这个content是post的话是类似“a=88&b=99”这样的字符串吗?
测试了好几次了。。

回答:

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {

RequestBody formBody = new FormBody.Builder()

.add("search", "Jurassic Park")

.build();

Request request = new Request.Builder()

.url("https://en.wikipedia.org/w/index.php")

.post(formBody)

.build();

Response response = client.newCall(request).execute();

if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());

}

回答:

请问你这个问题解决了吗 我也遇到了同样的问题

以上是 OKHttp 3如何设置post请求编码?我用了RequestBody.create方法 的全部内容, 来源链接: utcz.com/p/170283.html

回到顶部