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

后端 未结 6 1457
心在旅途
心在旅途 2021-01-31 02:01

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



        
6条回答
  •  猫巷女王i
    2021-01-31 02:32

    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.

提交回复
热议问题