gpt4 book ai didi

android - 删除 Android 中特定改造请求的 header

转载 作者:行者123 更新时间:2023-12-05 08:18:08 25 4
gpt4 key购买 nike

设置这种拦截器后是否有办法删除特定 header :

public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()
val request: Request = original.newBuilder()
.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken) //<-- need to remove this one for only one request
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
.method(original.method(), original.body())
.build()
return chain.proceed(request)

这是我的 Retrofit 实例:

object RetrofitClientInstance {

private val httpClient = OkHttpClient.Builder()
.addInterceptor(AuthInterceptor())
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))

private val retrofit = Retrofit.Builder()
.baseUrl(AppConstant.SERVER_BETA)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()

fun <T> getRetrofitInstance(service: Class<T>): T {
return retrofit.create(service)
}
}

这是我的 API 服务:

interface ApiService {
@GET("/app/shoes")
fun getShoes() : Call<Shoes>
}

感谢您的帮助:)

最佳答案

反之亦然。向 API 调用添加 header ,指示是否添加 auth header 。像这样:

interface ApiService {
@Headers("isAuthorizable: false")
@GET("/app/Socks")
fun getShoes() : Call<Socks>

@GET("/app/shoes")
fun getShoes() : Call<Shoes>

@GET("/app/sandals")
fun getShoes() : Call<Sandals>
}

检查 Interceptor 中的 header ,如果满足条件则添加 header 。像这样:

public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()

val shouldAddAuthHeaders = original.headers["isAuthorizable"] != "false"

val requestBuilder = request
.newBuilder()
.method(request.method, request.body)
.removeHeader("isAuthorizable")

if (shouldAddAuthHeaders) {
requestBuilder.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken)
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
}

return chain.proceed(requestBuilder.build())
}
}

注意:使用相同的逻辑,可以指定要为其添加身份验证 header 的请求,而不是过滤掉不需要身份验证的请求。也就是说,

interface ApiService {
@GET("/app/Socks")
fun getShoes() : Call<Socks>


@Headers("isAuthorizable: true")
@GET("/app/shoes")
fun getShoes() : Call<Shoes>

@Headers("isAuthorizable: true")
@GET("/app/sandals")
fun getShoes() : Call<Sandals>
}
public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()

val shouldAddAuthHeaders = original.headers["isAuthorizable"] == "true"

val requestBuilder = request
.newBuilder()
.method(request.method, request.body)
.removeHeader("isAuthorizable")

if (shouldAddAuthHeaders) {
requestBuilder.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken)
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
}

return chain.proceed(requestBuilder.build())
}
}

关于android - 删除 Android 中特定改造请求的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64459465/

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