添加统一通用header, 不会覆盖前面的header
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.addHeader("header-key", "value1")
.addHeader("header-key", "value2")
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
添加统一通用header, 会覆盖前面的header
okHttpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("headerkey", "header-value")// <-- this is the important line
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
统一添加参数
OkHttpClient.Builder httpClient =
new OkHttpClient.Builder()
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request()
HttpUrl originalHttpUrl = original.url()
HttpUrl url = originalHttpUrl.newBuilder()
.addQueryParameter("apikey", "your-actual-api-key")
.build()
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.url(url)
Request request = requestBuilder.build()
return chain.proceed(request)
}
})
有两种方式可以实现这一 *** 作:
1:nginx反向代理(需要两个nginx服务)
在nginx反向代理服务器通过使用proxy_set_header实现。
e. proxy_set_header X-Forwarded-For 127.0.0.1
2:安装模块Passenger
[Passenger用户手册] https://github.com/biti/passenger-doc-zh/wiki/Passenger%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8Cnginx%E7%89%88
安装完成后使用:
passenger_set_cgi_param HTTP_X_FORWARDED_FOR 127.0.0.1
来增加请求header中的参数
应用场景:
1. 切流,一个线上服务,新增了一个功能,需要公网内部测试,这时候可以根据来源ip &path设置不同的header信息,配合业务逻辑实现内外网流量切分,保证内测功能只有内网可见。
2. 使用非标准标头向上游服务器通知用户的IP地址和其他请求属性
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)