• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to test if a URL is accessible  RSS feed

 
Rajasekhar Devi Reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to write a code that acepts a url (can be any protocol) and tests if the URL is accessible(either to read or to write).

Because I dont know what the protocol is I just used java.net.URL to accept a url, created URLConnection by callong url.openConnection() and then made a connection by calling



if an exception is fired I print out that the connection could not be made.

Now my question is, my assumption that above code not returning exception will imply a connection that can be used to either read or write, correct ?

what if the url requires a user name and password ?

and I dont see a method to close the connection in API for URLConnection. Is it fine because the host on the other end closes connection after certain period of inactivity? is it true for any protocol ?
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To check whether a particular resource is allowed to be accessed I think you will have to request the resource with the particular protocol and decide based on the response given. May be the protocol may support querying accessible status of the particular resource.

For example in HTTP you may get an HTTP status 401 if you tried to access a privileged resource. Also in HTTP you may be able to use the HEAD request with an HTTP GET to obtain meta information about the resource.

The Abstract class URLConnection method getInputStream returns an InputStream to get at the byte stream the sever sends. By closing this Input stream you may be able to close the network resources such as the connection, unless the particular protocol implementation of the URLConnection defines a different behavior. As an example only, an HTTP implementation of the URLConnection class can choose not to close the inputstream if there are other resource embedded in an HTML page until some other condition is satisfied. Check here for the URLConnection API which describes some of these points.

Now my question is, my assumption that above code not returning exception will imply a connection that can be used to either read or write, correct ?


Your assumption may be correct here. If you are not able to make a connection one reason could be that you do not have permission to make a connection at the network level. Making a connection successfully will probably allow you read and write to the socket(e.g. make a request, get an error response), but, it will not guarantee access to the resource you are looking for as there will be permission restrictions at an server application level.
 
Rajasekhar Devi Reddy
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you verymuch Gamini Sirisena, for the response I will try things that you have suggested.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!