• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get the HTTP Response code when a user has provided a invalid url.

 
Pradeep Chaubey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep Chaubey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice catch Rob. Thank you very much indeed.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic