使用翻新2向请求添加标头

编程入门 行业动态 更新时间:2024-10-25 06:23:17
本文介绍了使用翻新2向请求添加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试发送带有身份验证标头的请求,但是服务器似乎无法识别客户端. 我使用了本教程,并实现了如下拦截器:

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; } @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.服务器人员告诉我,我缺少这些参数:(例如,邮差收到了)

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@email", "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向请求添加标头

本文发布于:2023-10-29 10:24:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!