Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP and HTTPS default port number constants in java [duplicate]

Usually Java comes along with a large set of sensible constants to use. HTTP and HTTPS default port numbers as assigned by IANA (80 and 443) would be sensible constants. I checked the javadoc of java.net.URL, java.net.HttpURLConnection and javax.net.ssl.HttpsURLConnection but didn’t find them there. Are those constants somewhere in the JavaSE? If not, are they somewhere available in the classes a web application on Tomcat has access to, e.g. org.apace.catalina or coyote? Just because I don’t like magic numbers in my code…

Do I have to do this in URL composition?

if(port != new URL("http://example.com/").getDefaultPort() &&
        port != new URL("https://example.com/").getDefaultPort()){
    stringBuilder.append(":");
    stringBuilder.append(port);
}
like image 426
Matthias Ronge Avatar asked Dec 30 '25 18:12

Matthias Ronge


1 Answers

Apache HttpClient has them, if this is a library you use:

  • http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpURL.html#DEFAULT_PORT
  • http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpsURL.html#DEFAULT_PORT
like image 108
Thilo Avatar answered Jan 02 '26 09:01

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!