Apache HttpClient and custom ports

前端 未结 3 1271
遥遥无期
遥遥无期 2021-02-06 17:15

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.

相关标签:
3条回答
  • 2021-02-06 17:46

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

    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.

    0 讨论(0)
  • 2021-02-06 17:51

    Another approach is to configure httpClient to use a custom 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 HttpPost and calling httpClient.execute(host, httpPost, handler, context), only finding your port is appended after the path, like: http://localhost/api:8080, which is wrong.

    0 讨论(0)
  • 2021-02-06 18:07

    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.

    0 讨论(0)
提交回复
热议问题