How to specify a default user agent for okhttp 2.x requests

时光怂恿深爱的人放手 提交于 2019-12-03 03:01:02

问题


I am using okhttp 2.0 in my Android app and didn't find a way to set some common User Agent for all outgoing requests.

I thought I could do something like

OkHttpClient client = new OkHttpClient();
client.setDefaultUserAgent(...)

...but there's no such method or similar. Of course I could provide some extension utility method which would wrap a RequestBuilder to attach .header("UserAgent") and then I would use it for building all my requests, but I thought maybe I missed some existing and simpler way?


回答1:


You can use an interceptor to add the User-Agent header to all your requests.

For more information about okHttp interceptors see https://github.com/square/okhttp/wiki/Interceptors

Example implementation of this interceptor:

/* 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);
    }
}

Test for the 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"));
}



回答2:


OkHttp v2.1 which is set to be released in the next few weeks will automatically set a User-Agent header if one is not already set.

As of now there isn't a good way to add this header to every request in a centralized way. The only workaround is to include the header manually for every Request that is created.




回答3:


In case anyone is looking for this working with OkHttp 3 and in Kotlin:

val client = OkHttpClient.Builder()
    .addNetworkInterceptor { chain ->
      chain.proceed(
          chain.request()
              .newBuilder()
              .header("User-Agent", "COOL APP 9000")
              .build()
      )
    }
    .build()



回答4:


Using an intercepter is no longer required in the newer versions of OkHttp. Adding a user agent is as simple as:

Request request = new Request.Builder()
    .url("http://www.publicobject.com/helloworld.txt")
    .header("User-Agent", "OkHttp Example")
    .build();

Source: OkHttp wiki.




回答5:


Based on @josketres answer, here is a similar Interceptor for OkHttp version 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);
    }
}

Plus the updated test:

@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"));
}


来源:https://stackoverflow.com/questions/26509107/how-to-specify-a-default-user-agent-for-okhttp-2-x-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!