如何在没有请求体的情况下发出 OKHTTP post 请求?

How to make OKHTTP post request without a request body?

有什么方法可以使用没有请求主体的 OkHTTP 发出 post 请求吗?

    RequestBody reqbody = RequestBody.create(null, new byte[0]);  
    Request.Builder formBody = new Request.Builder().url(url).method("POST",reqbody).header("Content-Length", "0");
    clientOk.newCall(formBody.build()).enqueue(OkHttpCallBack());

这对我有用:

RequestBody body = RequestBody.create(null, new byte[]{});
  • "".toRequestBody()
  • "".toRequestBody("application/json".toMediaTypeOrNull())

...取决于您的端点期望的媒体类型。

我正在使用 okhttp3

您也可以对空请求正文执行此操作:

val empty: RequestBody = EMPTY_REQUEST

对于 POST:

val request = Request.Builder().url(http).post(EMPTY_REQUEST).build()

在 okhhtp3 中不推荐使用带有参数参数的以下方法。

RequestBody.create(null, new byte[0])

以下解决方案对我有用。

RequestBody.create("",null)) 

Request.Builder().url(errorRetryUrl).post(RequestBody.create("",null)).build()