问题描述
我正在使用 Apache HttpClient 4,它运行良好.唯一不起作用的是自定义端口.好像是获取了根目录,忽略了端口.
I'm using the Apache HttpClient 4 and it works fine. The only thing that doesn't work is custom ports. It seems like the root directory is fetched and the port is ignored.
HttpPost post = new HttpPost("http://myserver.com:50000");
HttpResponse response = httpClient.execute(post);
如果没有定义端口,http- 和 https-connections 运行良好.方案注册表定义如下:
If no port is defined, http- and https-connections work well.The scheme registry is defined as follows:
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);
如何为请求定义自定义端口?
How can I define custom ports for a request?
推荐答案
问题是服务器不理解 HTTP 1.1 分块传输.我使用 ByteArrayEntity 缓存了数据,一切正常.
The problem was that the server does not understand HTTP 1.1 chunked transfers. I cached the data by using a ByteArrayEntity and all was ok.
因此自定义端口确实适用于上述代码.
So custom ports do work with the code mentioned above.
这篇关于Apache HttpClient 和自定义端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!