I'm trying to send requests with authentication headers, but it seems that the server cannot identify the client.I used this tutorial, and implemented an interceptor as follows:public class AuthenticationInterceptor implements Interceptor {private String authId;private String authToken;public AuthenticationInterceptor(String authId, String authToken) { this.authId = authId; this.authToken = authToken;}@Overridepublic Response intercept(@NonNull Chain chain) throws IOException { Request original = chain.request(); Request.Builder builder = original.newBuilder(); if (authId != null && authToken != null) { Timber.d("adding auth headers for: " + this); builder.header("auth_id", authId) .header("auth_token", authToken); } Request request = builder.build(); return chain.proceed(request); }}当我尝试向服务器发送经过身份验证的请求时,它返回错误响应409.服务器人员告诉我,我缺少这些参数:(例如,邮差收到了)When I'm trying to send authenticated requests to the server, it returns error response 409. The server guy told me that I'm missing those params: (which received by Postman for instance) "accept": [ "*/*" ], "accept-encoding": [ "gzip, deflate" ], "cookie": [ "PHPSESSID=ah1i1856bkdln5pgmsgjsjtar3" ] 我认为使用Dagger2可能会导致此问题(请参见此处),因此我隔离了okHttpClient ,但仍然无法正常工作.I thought using Dagger2 might cause this issue (see here), so I've isolated the okHttpClient, but it still doesn't work.这是我的用法实现(非常简单):Here is my usage implementation (very straightforward):Retrofit retrofit;OkHttpClient client;AuthenticationInterceptor authenticationInterceptor;HttpLoggingInterceptor loggingInterceptor;private void testHeaders() {loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { Timber.i(message); }});loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);client = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .build();retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .client(client) .baseUrl(BuildConfig.SERVER_ADDRESS) .build();retrofit.create(EntrOnline.class).getLoginToken("[email protected]", "XXX").enqueue(new Callback<NewAccount>() { @Override public void onResponse(Call<NewAccount> call, Response<NewAccount> response) { authenticationInterceptor = new AuthenticationInterceptor(response.body().getAuthId(), response.body().getAuthToken()); client = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .addInterceptor(authenticationInterceptor) .build(); retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .client(client) .baseUrl(BuildConfig.SERVER_ADDRESS) .build(); retrofit.create(EntrOnline.class).getKeys("50022d8a-b309-11e7-a902-0ac451eb0490").enqueue(new Callback<List<NewEkey>>() { @Override public void onResponse(Call<List<NewEkey>> call, Response<List<NewEkey>> response) { Timber.d("Test Api"); } @Override public void onFailure(Call<List<NewEkey>> call, Throwable t) { } }); } @Override public void onFailure(Call<NewAccount> call, Throwable t) { }});}谢谢!推荐答案看起来像服务器问题.您可以尝试在拦截器中添加他所谈论的标头.Looks like a server issue.You can try adding the headers he talked about in your interceptor.builder.addHeader("Accept", "*/*").addHeader("accept-encoding", "gzip, deflate")但是应该在网络上使用cookie来跟踪当前会话.该API上不需要.But the cookie should be used on the web to keep track of the current session. Shouldn't be needed on the API. 这篇关于使用翻新2向请求添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 07:37