port in URL constructor
java
Solution
See this example gives default ports for their respective services. I have shown http, and ftp here.
Eg:
public class Test {
public static void main(String[] args){
try {
System.out.println(new URL("http://www.ietf.org/rfc/rfc2396.txt").getDefaultPort());
System.out.println(new URL("ftp://www.ietf.org/rfc/rfc2396.txt").getDefaultPort());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Problem
I want to check which port is used, when the port is not explicitly specified in the URL constuctor. SO here is a code. ``` URL url = new URL("http://www.ietf.org/rfc/rfc2396.txt"); System.out.println("URL :"+url.getPort()); ``` getPort() returned -1. It indicates that I have not set the port thats why grtPort() had returned the -1 value. In java doc of java.net.URL class "If the port is not specified in URL, the default port for the protocol is used instead. The default port for http is 80." Then in the above case it should have returned the 80 default port. Isn't it ? But it is not. So how do I know which port is being used for the connection?