我在Android应用中使用okhttp 2.0,并没有找到为所有传出请求设置常用用户代理的方法。
我以为我可以做点什么
OkHttpClient client = new OkHttpClient();
client.setDefaultUserAgent(...)
......但是没有这样的方法或类似方法。
当然我可以提供一些扩展实用程序方法,它会将RequestBuilder包装起来.header("UserAgent")
,然后我会用它来构建我的所有请求,但我想也许我错过了一些现有的更简单的方法?
答案 0 :(得分:80)
您可以使用拦截器将User-Agent标头添加到您的所有请求中。
有关okHttp拦截器的更多信息,请参阅https://github.com/square/okhttp/wiki/Interceptors
此拦截器的示例实现:
/* This interceptor adds a custom User-Agent. */
public class UserAgentInterceptor implements Interceptor {
private final String userAgent;
public UserAgentInterceptor(String userAgent) {
this.userAgent = userAgent;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithUserAgent = originalRequest.newBuilder()
.header("User-Agent", userAgent)
.build();
return chain.proceed(requestWithUserAgent);
}
}
测试UserAgentInterceptor:
public void testUserAgentIsSetInRequestHeader() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("OK"));
server.play();
String url = server.getUrl("/").toString();
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new UserAgentInterceptor("foo/bar"));
Request testRequest = new Request.Builder().url(url).build()
String result = client.newCall(testRequest).execute().body().string();
assertEquals("OK", result);
RecordedRequest request = server.takeRequest();
assertEquals("foo/bar", request.getHeader("User-Agent"));
}
答案 1 :(得分:8)
OkHttp v2.1设置为在未来几周内发布will automatically set User-Agent
标头(如果尚未设置)。
截至目前,没有一种好方法可以集中方式将此标头添加到每个请求中。唯一的解决方法是为每个创建的Request
手动包含标题。
答案 2 :(得分:6)
答案 3 :(得分:3)
基于@josketres答案,这是 OkHttp版本3
的类似拦截器public class UserAgentInterceptor implements Interceptor {
private final String mUserAgent;
public UserAgentInterceptor(String userAgent) {
mUserAgent = userAgent;
}
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request().newBuilder().header("User-Agent", mUserAgent).build();
return chain.proceed(request);
}
}
加上更新的测试:
@Test
public void testUserAgentIsSetInRequestHeader() throws IOException, InterruptedException {
final String expectedUserAgent = "foo/bar";
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("OK"));
server.start();
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
okHttpBuilder.addInterceptor(new UserAgentInterceptor(expectedUserAgent));
Request request = new Request.Builder().url(server.url("/").url()).build();
ResponseBody result = okHttpBuilder.build().newCall(request).execute().body();
assertNotNull(result);
assertEquals("OK", result.string());
assertEquals(expectedUserAgent, server.takeRequest().getHeader("User-Agent"));
}
答案 4 :(得分:2)
万一有人在使用OkHttp 3和Kotlin来寻找它,
val client = OkHttpClient.Builder()
.addNetworkInterceptor { chain ->
chain.proceed(
chain.request()
.newBuilder()
.header("User-Agent", "COOL APP 9000")
.build()
)
}
.build()
答案 5 :(得分:0)
您必须在较新的版本中使用builder
。 (2020年9月)
client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request originalRequest = chain.request();
Request requestWithUserAgent = originalRequest.newBuilder()
.header("User-Agent", "My Agent is so cool")
.build();
return chain.proceed(requestWithUserAgent);
}
})
.build();