3

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); 

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?

3
  • Erm ... how are you supposing to connect to port 50000, when your sockets are listening on port 80 and 443? Should this not be final Scheme http = new Scheme("http", 50000, PlainSocketFactory.getSocketFactory()); ? Commented Oct 10, 2011 at 13:55
  • I also thought like this, but the scheme port is only the standard when no port is given. If a port has explicitly been set, that port will be used instead of the scheme port. Commented Oct 10, 2011 at 14:42
  • 1
    I am running into the same issue of the client not being able to handle custom ports. Could you give an example of how you used the ByteArrayEntity ? Commented Nov 10, 2011 at 18:05

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If you're getting "400 bad request", it means that you are actually connecting to the server, sending the request and getting the response. Check your server logs to see what the issue may be.
Like this way of constructing httpHost. It segregates the host address and a full url can be obtained like uri.toURL().toString(). Also, it makes URL interpolation correct in httpClient.execute(httpHost, httpRequest, handler, context).
2

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.