Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient and custom ports

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?

like image 564
Stephan Avatar asked Feb 02 '26 03:02

Stephan


2 Answers

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.

like image 86
Aleks G Avatar answered Feb 04 '26 18:02

Aleks G


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.

like image 20
Stephan Avatar answered Feb 04 '26 18:02

Stephan