我正在使用 Apache HttpClient 4,它工作正常。唯一不起作用的是自定义端口。似乎获取了根目录并忽略了端口。
HttpPost post = new HttpPost("http://myserver.com:50000");
HttpResponse response = httpClient.execute(post);
如果未定义端口,则 http- 和 https- 连接可以正常工作。方案注册表定义如下:
final SchemeRegistry sr = new SchemeRegistry();
final Scheme http = new Scheme("http", 80,
PlainSocketFactory.getSocketFactory());
sr.register(http);
final SSLContext sc = SSLContext.getInstance(SSLSocketFactory.TLS);
sc.init(null, TRUST_MANAGER, new SecureRandom());
SSLContext.setDefault(sc);
final SSLSocketFactory sf = new SSLSocketFactory(sc,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
final Scheme https = new Scheme("https", 443, sf);
sr.register(https);
如何为请求定义自定义端口?