如何使用OkHttp更改连接请求的标头

huangapple go评论82阅读模式
英文:

How to change the header of connect request with okhttp

问题

我安装了一个拦截器,它在我的 Java OkHttp4 客户端上设置了自定义的用户代理字符串。

```java
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();

我使用 Fiddler 进行了检查,它似乎对请求(GET/POST)本身起作用。然而,在此之前有一个 CONNECT 请求,其仍然具有 okhttp 标头。我该如何更改 CONNECT 请求的 User-Agent 标头?

如何使用OkHttp更改连接请求的标头


<details>
<summary>英文:</summary>

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?

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/eI0EP.png

</details>


# 答案1
**得分**: 1

我通过添加自定义代理验证器最终解决了我的问题。

```java
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();
    }
}
英文:

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(&quot;CONNECT&quot;, null)
                    .header(&quot;Host&quot;, toHostHeader(route.address().url(), true))
                    .header(&quot;Proxy-Connection&quot;, &quot;Keep-Alive&quot;)
                    .build();
        }

        return request.newBuilder()
                .header(&quot;User-Agent&quot;, MYUSERAGENT)
                .build();
    }
}

huangapple
  • 本文由 发表于 2020年10月8日 15:58:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64258181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定