我可以在 JGit 中使用 clone 命令克隆 repo
Repo 是 http,当然当我在代理后面时它无法克隆
你能帮我写代码示例如何在 java 中使用代理配置 JGit
谢谢!
最佳答案
JGit 使用标准的ProxySelector
机制来进行Http 连接。
从今天开始,框架使用的字段 org.eclipse.jgit.transport.TransportHttp.proxySelector
不可覆盖。不过,它是可配置的,可以自定义 JVM 默认代理选择器,如下所示:
ProxySelector.setDefault(new ProxySelector() {
final ProxySelector delegate = ProxySelector.getDefault();
@Override
public List<Proxy> select(URI uri) {
// Filter the URIs to be proxied
if (uri.toString().contains("github")
&& uri.toString().contains("https")) {
return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress
.createUnresolved("localhost", 3128)));
}
if (uri.toString().contains("github")
&& uri.toString().contains("http")) {
return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress
.createUnresolved("localhost", 3129)));
}
// revert to the default behaviour
return delegate == null ? Arrays.asList(Proxy.NO_PROXY)
: delegate.select(uri);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
if (uri == null || sa == null || ioe == null) {
throw new IllegalArgumentException(
"Arguments can't be null.");
}
}
});
关于java - 代码中的 JGit 代理配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16921374/