gpt4 book ai didi

android - 使用基本 HTTP 身份验证 : "Cannot retry streamed HTTP body" 改造 POST 请求

转载 作者:IT老高 更新时间:2023-10-28 22:12:02 27 4
gpt4 key购买 nike

我正在使用 Retrofit 执行一个基本的 POST 请求,并且我正在为该请求提供一个基本的 @Body。

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

当我为 Retrofit 构建接口(interface)时,我提供了我自己的自定义 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);
}

当我直接使用 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 $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. 再次尝试重新发送请求,然后抛出错误。

不知道如何解决这个问题。任何帮助都会很棒。

最佳答案

谢谢,杰西。

以防万一,这是我为基本身份验证所做的代码。

首先,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);

然后是ApiRequestInterceptor:

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

关于android - 使用基本 HTTP 身份验证 : "Cannot retry streamed HTTP body" 改造 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21370620/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com