我有实现Authenticator接口的ProxyAuthenticator类。
public class ProxyAuthenticator implements Authenticator {
private String proxyUser;
private String proxyPassword;
public ProxyTessiAuthenticator(String proxyUser, String proxyPassword) {
this.proxyUser = proxyUser;
this.proxyPassword = proxyPassword;
}
@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return authenticate(proxy, response);
}
@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
String credential = Credentials.basic(proxyUser, proxyPassword);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
}
我在com.squareup.ok中得到了这个异常http:
java.net.ProtocolException:后续请求太多:21
如果您已经尝试过,则需要短路其他身份验证请求。
https://github.com/square/okhttp/blob/3a6646dfd61cf55e3db38ebd08c7e504dde7a4bd/docs/recipes.md#handling-身份验证kt-java
https://howtoprogram.xyz/2016/11/03/basic-authentication-okhttp-example/
public Request authenticate(Route route, Response response) throws IOException {
String credential = ...
if (responseCount(response) >= 3) {
return null;
}
return response.request().newBuilder().header("Authorization", credential).build();
}