改造POST请求瓦特/基本HTTP认证:"不能重试流HTTP身体QUOT; [英] Retrofit POST request w/ Basic HTTP Authentication: "Cannot retry streamed HTTP body"

查看:261
本文介绍了改造POST请求瓦特/基本HTTP认证:"不能重试流HTTP身体QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是改造做一个基本的POST请求,而我提供一个基本的@Body的请求。

I'm using Retrofit to do a basic POST request, and I'm providing a basic @Body for the request.

@POST("/rest/v1/auth/login")
LoginResponse login(@Body LoginRequest loginRequest);

当我建立的改造我提供我自己的自定义OkHttpClient接口,和所有我做它是添加自己的自定义验证:

When I'm building the interface for Retrofit I'm providing my own custom OkHttpClient, and all that I'm doing to it is adding my own custom authentication:

    @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);
    }

这个伟大的工程,当我直接发送请求与OKHttp,并与改造等GET请求,但是当我用改造做我得到以下错误的POST请求:

This works great when I'm sending requests directly with OKHttp, and other GET requests with retrofit but when I use retrofit to do a POST request I get the following error:

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 $Proxy3.login(Native Method)
            at com.audax.paths.job.LoginJob.onRunInBackground(LoginJob.java:41)
            at com.audax.library.job.AXJob.onRun(AXJob.java:25)
            at com.path.android.jobqueue.BaseJob.safeRun(BaseJob.java:108)
            at com.path.android.jobqueue.JobHolder.safeRun(JobHolder.java:60)
            at com.path.android.jobqueue.executor.JobConsumerExecutor$JobConsumer.run(JobConsumerExecutor.java:172)
            at java.lang.Thread.run(Thread.java:841)

我研究了一下它。如果我删除了认证,并指向不需要身份验证的服务器,然后正常工作。

I've played around with it. If I remove the authentication, and point to a server that doesn't require the authentication, then it works fine.

  1. 所以,我一定要发送的信息。
  2. 获得认证的挑战要求。
  3. 应对这一挑战的要求。
  4. 试图重新发送的请求,并且然后错误被抛出。

不知道如何来解决这个问题。任何帮助将是美好的。

Not sure how to get around this. Any help would be wonderful.

推荐答案

您最好的选择是提供您的凭据通过<一个改造href="http://square.github.io/retrofit/javadoc/retrofit/RequestInterceptor.html">RequestInterceptor而不是OkHttp的 OkAuthenticator 。这个接口工作最好的时候,请求可以重试,但在你的情况下,我们已经扔出去后身体的时候,我们发现这是有必要的。

Your best bet is to provide your credentials to Retrofit via a RequestInterceptor instead of OkHttp's OkAuthenticator. That interface works best when the request can be retried, but in your case we've already thrown out the post body by the time we find out that's necessary.

您可以继续在要求的格式使用OkAuthenticator的凭证类,它可以连接code你的用户名和密码。你想要的头名是批准

You can continue to use OkAuthenticator's Credential class which can encode your username and password in the required format. The header name you want is Authorization.

这篇关于改造POST请求瓦特/基本HTTP认证:&QUOT;不能重试流HTTP身体QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆