6

I've hunted around a bit trying to see if port 80 and port 443 are defined as public constants anywhere. Do these exist in the JDK (or perhaps in a common library such as Apache HttpClient)?

hertzsprung
  • 8,558
  • 4
  • 36
  • 72

2 Answers2

9

Look at Javadoc for URL: http://docs.oracle.com/javase/8/docs/api/java/net/URL.html#getDefaultPort--

getDefaultPort() returns the port for the given protocol

URL url = new URL("http://blah.com");
int defaultPort = url.getDefaultPort();
Paolo Forgia
  • 6,202
  • 8
  • 42
  • 57
Aksel Willgert
  • 11,067
  • 5
  • 50
  • 72
4

Apache HttpClient does have them as integer constants:

Thilo
  • 250,062
  • 96
  • 490
  • 643
  • I cannot find them in the much refactored version 4 – Thilo Jul 17 '13 at 08:02
  • 1
    in apache v4, it uses `DefaultSchemePortResolver.resolve(HttpHost host)` which returns the integer for the port number, given the schema -- https://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/org/apache/http/impl/conn/DefaultSchemePortResolver.html – Unglued Sep 10 '15 at 17:50