How to detect the web page is available or not?????

Hi:
   Here I have a question about java. Fot the program to open an web page, before is open it, it must chekc the page can be opened ot not. I use getContentLength() to get the size, if it is < 0 then treat it as unrreachable, otherwise use .showdocument (url,) to open it. It work in the case of the auto directed program and the destination page at the same server. If ther are in different sever, it return size <0 but the ppage is available. Can anybody give idea why it like this.
   Or maybe there has other way to do it???
   Millions of thanks in advance!!

Best Regard

wky086Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Venci75Connect With a Mentor Commented:
regarding the response code - use
urlconn.setResponseCode(); method of the HttpUrlConnection object
0
 
Venci75Commented:
the http response code must be 404 when the page is not present
0
 
wky086Author Commented:
Sorry , but which method in java is used to get the error code?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
You can't rely on getContentLength() to give you the correct data. You *could* send a HEAD request first, but why not do a GET right away? The only rationale I can think of for checking first is if you expect the majority of requests to fail.
0
 
wky086Author Commented:
But How to use Get to send the request. Sorry I have not idea about it. Can you give me some hint in code. in html or javascript is OK.
I test with this code
try {
URL url =new URL(URLName);

URLConnection urlconn = url.openConnection();
urlconn.connect();
HttpURLConnection httpconn =(HttpURLConnection)urlconn;
HttpResult = httpconn.getResponseCode();
if(HttpResult == HttpURLConnection.HTTP_OK)
....
}
catch (){
}
it catch exception. Do you have any idea about this code
Millions of thanks in advance
0
 
Venci75Commented:
URL url =new URL(URLName);
HttpURLConnection urlconn = (HttpURLConnection)urlconn.setRequestMethod("GET");
url.openConnection();
...
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to Venci75

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0
 
CEHJCommented:
Of course, correct operation of Venci75's suggestion relies on correct server behaviour, which is an assumption you can't make IMHO. A better way would be to open an input stream on the URLConnection.
0
All Courses

From novice to tech pro — start learning today.