This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
"If the port is not specified in URL, the default port for the protocol is used instead.
the default port for http is 80."
but when i crete a URL object for www.degraeve.com/reference/urlencoding.php. and try to find which port is been used
using getPort(). I found that getPort() returns -1 which is a case when port is not set.
My confusion:
1. when port is not set or specified , Shouldn't getPort() return default port(80) since no port is specified in www.degraeve.com/reference/urlencoding.php? why it returns -1.
2. getPort() says that it returns -1 when port is not set. where to set the port ? Is it set by remote machine or in the URL ?
You have started with the question "Which port is being used?", but you haven't looked for the answer to that question. And besides, you already know the answer because you quoted what the API documentation specifically says about that. But instead you have substituted an entirely different question about setting the port. This question does not lead you to the answer to the question "Which port is being used?" -- at least not directly.
If getPort() returns -1, then as you already know, that means the port has not been set. And if you examine the API, you'll see that the only way for you to set the port is by specifying it in the constructor in the URL. And therefore if getPort() returns -1, that means you didn't specify the port in the URL. And the quote you posted from the class docs then tells you what you already knew was the answer.
Now if your question was meant to ask how you could find out that (for example) 80 was the default HTTP port via Java code, then I recommend the getDefaultPort() method.
after reading your reply i understand this.(* Please correct me if wrong anywhere).
Since i haven't set the port in URL constructor , that's why getPort() had returned the -1. If port is set to the 85 , getPort() will return the 85. Ok
but in docs it says that if port is not specified in URL then (HTTP )port 80 will be used by default. What about that?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Why are you saying "but in docs...", as if there were some kind of mismatch? Obviously, calling setPort explicitly would override the -1 value that causes the default port to be used.
ok. when HTTP protocoal is used port 80 is used as default.
then why getPort() have not returned 80 in my case?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Why would it return 80 if that port has not been specified? Don't read anything into the javadocs that isn't written in them - they say nothing about returning the default port. That's what the getDefaultPort method is for.
Thanks for keeping the patience .
The logic behind previous post is that "when no port is specified , then default port should be used".but it turns out that it is not the case
when a request is made for particular resource on internet using URL object. Some port must have to be used through which data can be transmitted. Isn't it ?
if yes then which port is used in following case
URL url = new URL("http://www.degraeve.com/reference/urlencoding.php");
System.out.println("port = " + url.getPort()); // -1
Yes, that's correct. Some port has to be used. And the quote you included in your very first post:
"If the port is not specified in URL, the default port for the protocol is used instead.
the default port for http is 80."
tells you which port will be used. You aren't missing anything. You are just making the incorrect assumption that the getPort() method tells you which port will be used. It doesn't do that, and its documentation doesn't say it will do that.
It means that port 80(default) is used. but as the docs for getPort() doesn't says anything about that. So getPort() does not return that port(80).
And since i have not set any port in the URL ,that's why getPort() returns -1.
naveen yadav wrote:It means that port 80(default) is used. but as the docs for getPort() doesn't says anything about that. So getPort() does not return that port(80).
And since i have not set any port in the URL ,that's why getPort() returns -1.
Is that a whole story ?
I don't think that the URL class has a fixed default port -- meaning that the default port is specified by the URLStreamHandler (the protocol handler). And it just so happens that the default port for the built in handler for http is set for port 80. If you configure the URL for other protocols, such as ftp or https; or if you use your own stream handler, you can have a default port that is not port 80.
Regardless, if you know that the port hasn't been set, you can always ask for the default port (from the handler) instead....
Paul Clapham wrote:................. You are just making the incorrect assumption that the getPort() method tells you which port will be used. It doesn't do that, and its documentation doesn't say it will do that.
i guess Paul was right. I was just reading too much in to the definition of getPort() and concluding it otherwise. my badwas i was making assumption that getPort() return the port which ever port is used even the default one which is not the case. getPort() returns the port which is explicitly and if not then returns -1. It does not return the default one in any case even when default port is used. There is a getDefaultPort() to return the default port.