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
  • 9,445
  • 4
  • 42
  • 77

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,572
  • 8
  • 46
  • 58
Aksel Willgert
  • 11,367
  • 5
  • 53
  • 74
4

Apache HttpClient does have them as integer constants:

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 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