aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes How to get the HTTP Response code when a user has provided a invalid url. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to get the HTTP Response code when a user has provided a invalid url." Watch "How to get the HTTP Response code when a user has provided a invalid url." New topic
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: 18154
    
    8

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
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

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 - OCEEJBD 6
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
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get the HTTP Response code when a user has provided a invalid url.
 
Similar Threads
Yahoo chat client, any ideas.......
Login into JSP through JAVA
Problem downloading an image using JSP
Uploading Image from Java Desktop App to Server
How to check whether urlconnection is active?