相关疑难解决方法(0)

使用基本HTTP身份验证更改POST请求:"无法重试流式HTTP正文"

我正在使用Retrofit来做一个基本的POST请求,我正在为请求提供一个基本的@Body.

@POST("/rest/v1/auth/login")
LoginResponse login(@Body LoginRequest loginRequest);
Run Code Online (Sandbox Code Playgroud)

当我为Retrofit构建界面时,我正在提供我自己的自定义OkHttpClient,而我正在做的就是添加我自己的自定义身份验证:

    @Provides
    @Singleton
    public Client providesClient() {
        OkHttpClient httpClient = new OkHttpClient();

        httpClient.setAuthenticator(new OkAuthenticator() {
            @Override
            public Credential authenticate(Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
                return getCredential();
            }

            @Override
            public Credential authenticateProxy(Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
                return getCredential();
            }
        });

        return new OkClient(httpClient);
    }
Run Code Online (Sandbox Code Playgroud)

当我使用OKHttp直接发送请求时,以及其他GET请求进行改造,但是当我使用改造来执行POST请求时,我会收到以下错误:

Caused by: java.net.HttpRetryException: Cannot retry streamed HTTP body
            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:324)
            at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:508)
            at com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:94)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:49)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:357)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:282)
            at …
Run Code Online (Sandbox Code Playgroud)

android http-authentication retrofit okhttp

25
推荐指数
4
解决办法
2万
查看次数

标签 统计

android ×1

http-authentication ×1

okhttp ×1

retrofit ×1