【问题标题】:OPTIONS/HEAD REST API request with Okhttp3使用 Okhttp3 的 OPTIONS/HEAD REST API 请求
【发布时间】:2017-08-04 22:25:03
【问题描述】:

我正在 Android 上编写一些 Rest 客户端,但遇到了一个问题 - 我不知道如何发出 HEAD 和 OPTIONS 请求。

OkHttp3中的GET/POST/PUT/DELETE/PATCH请求没有问题,基本上是这样的:

        request = new Request.Builder()
                .url(url)
                .headers(headerBuilder.build())
                .post(bodyBuilder.build())
                .build();

而且 OkHttp3 没有提供额外的方法,例如 head() 或 option()。

那么如何使用 OkHttp3 发出 HEAD 和 OPTIONS 请求?

【问题讨论】:

    标签: java android okhttp okhttp3


    【解决方案1】:

    找到答案,可能对其他人有用

    OkHttp3 还是有方法的

    Builder method(String method, RequestBody body)
    

    所以 OPTIONS 请求看起来像

            Request request = new Request.Builder()
                    .url(url)
                    .headers(headerBuilder.build())
                    .method("OPTIONS",requestBody)
                    .build();
    

    HEAD 相同

    【讨论】:

      【解决方案2】:

      看起来(至少在current implementation、API 3.12.0)、HEAD 请求可以像GET 和其他人一样进行:

      Request request = new Request.Builder()
                      .url(url)
                      .head()
                      .build();
      

      OPTION 仍然需要使用.method() 来实现

      【讨论】:

        猜你喜欢
        • 2020-09-16
        • 2015-02-12
        • 2018-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 1970-01-01
        • 2017-07-04
        相关资源
        最近更新 更多