Check if URL exists

B

ByteCoder

I can't seem to find the right method to check if a URL exists (it's not
a HTTP 404).

InputStream input = url.OpenStream();
input = new BufferedInputStream(input);

if(input.available() > 0) {
//...
}

doesn't seem to work. URLs that don't exist still pass the if
statement...?

Does anyone else know the solution to this problem?
 
B

ByteCoder

I can't seem to find the right method to check if a URL exists (it's
not a HTTP 404).

InputStream input = url.OpenStream();
input = new BufferedInputStream(input);

if(input.available() > 0) {
//...
}

doesn't seem to work. URLs that don't exist still pass the if
statement...?

Does anyone else know the solution to this problem?

I found the method:
if(url.openConnection().getContentLength() > 0) {
//...
}

How reliable is this method for any kind of URL content? For example: It
will return -1 if the content-length is unknown, but does that also mean
the file doesn't exist?
 
J

John C. Bollinger

ByteCoder said:
I can't seem to find the right method to check if a URL exists (it's not
a HTTP 404).

InputStream input = url.OpenStream();
input = new BufferedInputStream(input);

if(input.available() > 0) {
//...
}

doesn't seem to work. URLs that don't exist still pass the if
statement...?

Data (the error page) comes back on the input stream for a 404 error,
and, moreover, available() doesn't do quite what you think it does. All
in all, your approach will identify missing pages only by accident, and
will falsely identify existing pages as missing by the same accident.
Does anyone else know the solution to this problem?

Assuming you are using HTTP URLs, Have you tried testing

((HttpUrlConnection) url.openConnection()).getResponseCode()

?

Neither your method nor my suggestion accounts for cases where the
entire server is unavailable; chances are that you will be able to
handle an exception to detect that case.
 
B

ByteCoder

[...]
How reliable is this method for any kind of URL content? For example:
It will return -1 if the content-length is unknown, but does that also
mean the file doesn't exist?

Ok, I should have googled before I asked that question: According to the
W3C any content-length of 0 or greater is valid, unless a HTTP status
code is sent and in a few other (rare?) cases.
 
B

ByteCoder

Data (the error page) comes back on the input stream for a 404 error,
and, moreover, available() doesn't do quite what you think it does.
All in all, your approach will identify missing pages only by
accident, and will falsely identify existing pages as missing by the
same accident.


Assuming you are using HTTP URLs, Have you tried testing

((HttpUrlConnection) url.openConnection()).getResponseCode()

?

Neither your method nor my suggestion accounts for cases where the
entire server is unavailable; chances are that you will be able to
handle an exception to detect that case.

Thanks for the info.

I tested the getContent-Length method and it behaves correctly when the
URL is unavailable (and another page is returned).
 
F

Filip Larsen

"ByteCoder" wrote
I can't seem to find the right method to check if a URL exists (it's not
a HTTP 404).

I can only concur with John Bollinger:
HttpURLConnection.getResponseCode() seems to be the correct way to go in
all aspects (clear intention and exact fit with the HTTP specification).

If you experience that checking the contentLength also work even when
the server is returning an error page, I would guess it must be because
the HttpURLConnection implementation then delivers all content on the
error stream instead of the output stream on responses outside 200-299.


Best regards,
 
B

ByteCoder

"ByteCoder" wrote


I can only concur with John Bollinger:
HttpURLConnection.getResponseCode() seems to be the correct way to go
in all aspects (clear intention and exact fit with the HTTP
specification).

If you experience that checking the contentLength also work even when
the server is returning an error page, I would guess it must be
because the HttpURLConnection implementation then delivers all content
on the error stream instead of the output stream on responses outside
200-299.


Best regards,

It would appear to be that way yes.
 
Joined
Sep 19, 2008
Messages
3
Reaction score
0
hi,
can any one please say how to avoid/bypass the message/error box when checking for existence of url ....please let me know the method name...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top