在沙丁鱼中,如何将端口号更改为与端口80(对于HTTP)和443(对于HTTPS)不同的端口?
用户指南声明我必须“覆盖SardineImpl #createDefaultSchemeRegistry()或通过实例化SardineImpl提供您自己配置的HTTP客户端实例#SardineImpl(org.apache.http.impl.client.HttpClientBuilder)“但我找不到如何定义端口。
当我使用:
实例化SardineImpl时HttpClientBuilder builder = HttpClientBuilder.create();
SardineImpl sardine = new SardineImpl(builder, "user", "password");
byte[] data;
data = FileUtils.readFileToByteArray(new File("test.txt"));
sardine.put("http://webdav-server:8095/projects/", data);
我获得:
org.apache.http.NoHttpResponseException: webdav-server:8095 failed to respond
服务器可以通过浏览器访问,因此问题必须与端口的定义有关,我找不到如何执行此操作的示例。
有人可以帮我这个吗?提前谢谢。
答案 0 :(得分:1)
这是我在试图找到解决方案后绞尽脑汁想出来的。希望它可以帮助别人:
HttpClientBuilder builder = new HttpClientBuilder(){
@Override
public CloseableHttpClient build() {
SchemePortResolver spr = new SchemePortResolver() {
@Override
public int resolve(HttpHost httpHost) throws UnsupportedSchemeException {
return 8443; // SSL port to use
}
};
CloseableHttpClient httpclient = HttpClients.custom()
.useSystemProperties()
.setSchemePortResolver(spr)
.build();
return httpclient;
}
};
Sardine sardine = new SardineImpl(builder, "user", "passwd");
List<DavResource> resources = null;
resources = sardine.list("https://ftp-site.com/path/");
resources.forEach(resource -> {
System.out.println(resource.getName());
}
希望能帮助某人。