我正在使用Retrofit做一个基本的POST请求,我正在为请求提供一个基本的@Body。
@POST("/rest/v1/auth/login")
LoginResponse login(@Body LoginRequest loginRequest);
当我为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);
}
当我使用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)
我玩过它。如果我删除身份验证,并指向不需要身份验证的服务器,那么它可以正常工作。
不确定如何解决这个问题。任何帮助都会很棒。
答案 0 :(得分:39)
谢谢,杰西。
以防它有帮助,这是我为Basic 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);
然后是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;
}
}
答案 1 :(得分:15)
延伸Naren的回答:
您可以像这样建立身份验证String
:
String basicAuth = "Basic " + Base64.encodeToString(String.format("%s:%s", "your_user_name", "your_password").getBytes(), Base64.NO_WRAP);
然后您将basicAuth
传递给authorization
。
@GET("/user")
void getUser(@Header("Authorization") String authorization, Callback<User> callback)
答案 2 :(得分:14)
您最好的办法是通过RequestInterceptor代替OkHttp的OkAuthenticator
将您的凭据提供给Retrofit。当请求可以重试时,该接口效果最好,但在您的情况下,我们已经在发现必要时已经抛出了帖子正文。
您可以继续使用OkAuthenticator的Credential类,它可以按要求的格式对您的用户名和密码进行编码。您想要的标题名称是Authorization
。
答案 3 :(得分:6)
对于基本授权,您可以提供如下标题:
@GET("/user")
void getUser(@Header("Authorization") String authorization, Callback<User> callback)
答案 4 :(得分:1)
如果您使用最新版本的Retrofit / OkHttp进行此操作,目前的解决方案集是不够的。 Retrofit不再提供RequestInterceptor,因此您需要使用OkHttp的拦截器来完成类似的任务:
创建拦截器:
public class HttpAuthInterceptor implements Interceptor {
private String httpUsername;
private String httpPassword;
public HttpAuthInterceptor(String httpUsername, String httpPassword) {
this.httpUsername = httpUsername;
this.httpPassword = httpPassword;
}
@Override public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", getAuthorizationValue())
.build();
return chain.proceed(newRequest);
}
private String getAuthorizationValue() {
final String userAndPassword = "httpUsername" + ":" + httpPassword;
return "Basic " + Base64.encodeToString(userAndPassword.getBytes(), Base64.NO_WRAP);
}
}
您需要将拦截器添加到您的OkHttp客户端:
// Create your client
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpAuthInterceptor("httpUsername", "httpPassword"))
.build();
// Build Retrofit with your client
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.build();
// Create and use your service that now authenticates each request.
YourRetrofitService service = retrofit.create(YourRetrofitService.class);
我没有测试上面的代码,因此可能需要进行一些细微的修改。我现在在Kotlin for Android中编程 - 一天。