| Author |
How to get the HTTP Response code when a user has provided a invalid url.
|
Pradeep Chaubey
Greenhorn
Joined: Jun 15, 2009
Posts: 20
|
|
Dear members,
We have an application where a user will link a image to application. Sometime a user tries to associate a image which is from their own intranet and not on a open url. To handle such situation I tried to use response code from server which can be useful to handle such situation. I tried with a sample program but it throws exception and nothing is achieved. Is there any way around to get the response code from server even if a invalid URL is provided or know the reason exactly what was the problem.
My sample code to test this functionality is below :
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
Let me just point out that if the user provides a URL which does not connect to any server, it is unreasonable to expect to get a response code. That's because the response code is what the server sends as part of its response.
|
 |
Pradeep Chaubey
Greenhorn
Joined: Jun 15, 2009
Posts: 20
|
|
Thanks Paul. I will be using the exception type to recognize if a URL is invalid and display the appropriate error message to the user.
Thanks for your valuable opinion.
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18370
|
|
|
By the way, you don't need a URI to get a URL. You can change line 4 into this: That does mean that on line 8 you should now catch MalformedURLException instead of URISyntaxException.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Pradeep Chaubey
Greenhorn
Joined: Jun 15, 2009
Posts: 20
|
|
Nice catch Rob. Thank you very much indeed.
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18370
|
|
|
You're welcome.
|
 |
 |
|
|
subject: How to get the HTTP Response code when a user has provided a invalid url.
|
|
|