我正在使用 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);
如何为请求定义自定义端口?
一个建议是尝试使用HttpPost(URI address)
而不是那个String
范围。您可以显式设置端口:
URI address = new URI("http", null, "my.domain.com", 50000, "/my_file", "id=10", "anchor")
HttpPost post = new HttpPost(address);
HttpResponse response = httpClient.execute(post);
不能保证这会起作用,但请尝试一下。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)