java Apache HttpClient 和自定义端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7712505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:05:28  来源:igfitidea点击:

Apache HttpClient and custom ports

javaapache-httpclient-4.x

提问by Stephan

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.

我正在使用 Apache HttpClient 4,它工作正常。唯一不起作用的是自定义端口。似乎获取了根目录并忽略了端口。

HttpPost post = new HttpPost("http://myserver.com:50000");
HttpResponse response = httpClient.execute(post);

If no port is defined, http- and https-connections work well. The scheme registry is defined as follows:

如果未定义端口,则 http- 和 https-connections 运行良好。方案注册表定义如下:

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?

如何为请求定义自定义端口?

采纳答案by Stephan

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.

问题是服务器不理解 HTTP 1.1 分块传输。我使用 ByteArrayEntity 缓存了数据,一切正常。

So custom ports do work with the code mentioned above.

所以自定义端口确实可以与上面提到的代码一起使用。

回答by Aleks G

One suggestion is to try using HttpPost(URI address)instead of the one with Stringparameter. You can explicitly set the port:

一种建议是尝试使用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);

Can't guarantee this will work, but give it a try.

不能保证这会奏效,但请试一试。

回答by WesternGun

Another approach is to configure httpClientto use a custom SchemaPortResolver.

另一种方法是配置httpClient为使用自定义SchemaPortResolver.

int port = 8888;
this.httpClient = HttpClients.custom()
        .setConnectionManager(connectionManager)
        .setConnectionManagerShared(true)
        .setDefaultCredentialsProvider(authenticator.authenticate(url,
                port, username, password))
        .setSchemePortResolver(new SchemePortResolver() {
            @Override
            public int resolve(HttpHost host) throws UnsupportedSchemeException {
                return port;
            }
        })
        .build();

This way, you avoid problems of using a String to construct a HttpPostand calling httpClient.execute(host, httpPost, handler, context), only finding your port is appended afterthe path, like: http://localhost/api:8080, which is wrong.

这样,您可以避免使用 String 构造 aHttpPost并调用 的问题httpClient.execute(host, httpPost, handler, context),只会发现您的端口附加路径之后,例如: http://localhost/api:8080,这是错误的。