当前位置: 首页 > 知识库问答 >
问题:

更新POST请求W/Basic HTTP身份验证:“无法重试流式HTTP正文”

姜建德
2023-03-14
@POST("/rest/v1/auth/login")
LoginResponse login(@Body LoginRequest loginRequest);
    @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);
    }
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)
  1. 所以我必须发送信息。
  2. 正在获取身份验证质询请求。
  3. 正在响应质询请求。
  4. 试图重新发送请求,然后引发错误。

不知道怎么绕过这个。任何帮助都会很好。

共有1个答案

阎兴为
2023-03-14

谢了,杰西。

以防万一,下面是我为基本Auth所做的代码。

首先,MyApplication类中的init:

ApiRequestInterceptor requestInterceptor = new ApiRequestInterceptor();
requestInterceptor.setUser(user); // I pass the user from my model

ApiService apiService = new RestAdapter.Builder()
            .setRequestInterceptor(requestInterceptor)
            .setServer(Constants.API_BASE_URL)
            .setClient(new OkClient()) // The default client didn't handle well responses like 401
            .build()
            .create(ApiService.class);
import android.util.Base64;
import retrofit.RequestInterceptor;

/**
 * Interceptor used to authorize requests.
 */
public class ApiRequestInterceptor implements RequestInterceptor {

    private User user;

    @Override
    public void intercept(RequestFacade requestFacade) {

        if (user != null) {
            final String authorizationValue = encodeCredentialsForBasicAuthorization();
            requestFacade.addHeader("Authorization", authorizationValue);
        }
    }

    private String encodeCredentialsForBasicAuthorization() {
        final String userAndPassword = user.getUsername() + ":" + user.getPassword();
        return "Basic " + Base64.encodeToString(userAndPassword.getBytes(), Base64.NO_WRAP);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
 类似资料:
  • 我得到了一个使用fcm发送通知的应用程序,当我通过控制台发送消息时一切正常,但是当我尝试通过php(curl-post)或postman(firefox扩展)发送消息时,我得到了一个身份验证错误(甚至使用了密钥) 字符串(304)“{”错误:{”代码:401,“消息”:“请求缺少所需的身份验证凭据。应为OAuth 2访问令牌、登录cookie或其他有效身份验证凭据。请参阅https://devel

  • 输出: 为什么我得到200指示身份验证成功,但没有json返回给我必要的凭据?(如果我篡改授权标头,它会按预期返回403)。 我直接从一个成功的curl请求到同一个服务获取了请求头。为什么请求不返回任何内容? 成功的卷曲日志:

  • 我们的应用程序中有两个部分: 服务器-提供REST服务 客户端-通过Spring restTemplate使用它们 除了HTTP状态之外,我们的服务器还返回一个包含JSON的HTTP正文,详细描述了错误。所以,我在restTemplate中添加了自定义错误处理程序,以将一些编码为非错误的错误处理——这有助于很好地解析HTTP正文。 但在HTTP/1.1 401未经授权的情况下,我通过解析HTTP主

  • 问题内容: 我必须 使用 HTTP Basic身份验证从http服务器下载和解析XML文件。现在,我这样做: 但是以这种方式,我无法从具有http身份验证的服务器获取xml(或者我只是根本不知道该文档)文档。 如果您能向我展示实现我的目标的最好,最简单的方法,我将不胜感激。 问题答案: 您可以使用。例如: 这将设置默认值,并将在 所有 请求中使用。显然,当您不需要所有请求的凭据或多个不同的凭据(可

  • 我正在尝试向正在运行的本地服务器发出GET请求。我无法返回正确的数据,我看到“未经授权”的响应。如果字符串“token”是正确的,那么任何人都能发现任何明显的问题吗。 }* 我能够从命令行获得一个curl请求:

  • 我有一个授权服务器(http://localhost:8082)、资源服务器和隐式客户端(http://localhost:8080)项目。问题是,当客户端请求授权(令牌)时,身份验证服务器会显示登录屏幕,但在成功登录后,它会重定向到GEThttp://localhost:8082/而不是去http://localhost:8082/authorize?client_id=...(根据客户要求)