I installed an interceptor, which sets the custom useragent string on my java okhttp4 client.
public class UserAgentInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
return chain.proceed(chain.request().newBuilder()
.removeHeader("User-Agent")
.addHeader("User-Agent", MYUSERAGENT);
}
}
client = new OkHttpClient.Builder()
.addNetworkInterceptor(new UserAgentInterceptor())
.build();
I checked it with Fiddler and it seems to work with the request (GET/POST) itself. However, there is a CONNECT request beforehand which still has the okhttp header. How can I change the CONNECT User-Agent header?
I finally solved my problem by adding a custom proxy authenticator
new OkHttpClient.Builder()
.proxyAuthenticator(new MyProxyAuthenticator())
.addNetworkInterceptor(new UserAgentInterceptor());
public class MyProxyAuthenticator implements Authenticator {
@Nullable
@Override
public Request authenticate(@Nullable Route route, @NotNull Response response) throws IOException {
Request request = new JavaNetAuthenticator().authenticate(route, response);
if (request == null) {
request = new Request.Builder()
.url(route.address().url())
.method("CONNECT", null)
.header("Host", toHostHeader(route.address().url(), true))
.header("Proxy-Connection", "Keep-Alive")
.build();
}
return request.newBuilder()
.header("User-Agent", MYUSERAGENT)
.build();
}
}