OKHTTP3 设置代理ip用户名及密码

十有八⑨ 2017-12-15 02:46:12
okhttp3 如何设置代理IP 用户名及密码
...全文
1919 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
langzi001c 2019-11-15
  • 打赏
  • 举报
回复
这个怎么玩呀,小白,有demo吗,谢谢大佬
  • 打赏
  • 举报
回复
//https://square.github.io/okhttp/3.x/okhttp/okhttp3/Authenticator.htmlnew OkHttpClient()

.newBuilder()
.proxySelector(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
log.info("getHost:{}", uri.getHost());
if (uri.getHost().endsWith(host)) {
List<Proxy> proxyList = new ArrayList<>(4);
proxyList.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
return proxyList;
} else {
return null;
}
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
})
.proxyAuthenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
if (response.request().header("Proxy-Authorization") != null) {
// Give up, we've already failed to authenticate.
return null;
}

String credential = Credentials.basic(userName, password);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
})
.build();

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧