使用 Retrofit 2 添加标头以请求

2024-05-22

我正在尝试发送带有身份验证标头的请求,但服务器似乎无法识别客户端。 我用了this https://futurestud.io/tutorials/android-basic-authentication-with-retrofit教程,并实现了一个拦截器,如下所示:

public class AuthenticationInterceptor implements Interceptor {

private String authId;
private String authToken;

public AuthenticationInterceptor(String authId, String authToken) {
    this.authId = authId;
    this.authToken = authToken;
}

@Override
public 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。服务器人员告诉我,我缺少这些参数:(例如,邮递员收到的参数)

    “accept”: [
        “*/*”
    ],
    “accept-encoding”: [
        “gzip, deflate”
    ],
    “cookie”: [
        “PHPSESSID=ah1i1856bkdln5pgmsgjsjtar3"
    ]
  • 我认为使用 Dagger2 可能会导致这个问题(参见here https://stackoverflow.com/a/27868976/4449366),所以我隔离了okHttpClient,但是还是不行。

  • 这是我的使用实现(非常简单):

    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] /cdn-cgi/l/email-protection", "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) {
    
        }
    });
    

    }

Thanks!


@Headers("Accept: application/json")
@FormUrlEncoded
@POST("sendWalletMoney")
Call<WalletResponse> sendWalletMoney(@Field("sender") String sender,
                                     @Field("receiver") String receiver,
                                     @Field("amount") String amount,
                                     @Field("trnsx_type") String trnsx_type,
                                     @Field("currency") String currency,
                                     @Field("module_name") String module_name);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Retrofit 2 添加标头以请求 的相关文章

随机推荐