wky086
asked on
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
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
the http response code must be 404 when the page is not present
ASKER
Sorry , but which method in java is used to get the error code?
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.
ASKER
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)urlcon n;
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
I test with this code
try {
URL url =new URL(URLName);
URLConnection urlconn = url.openConnection();
urlconn.connect();
HttpURLConnection httpconn =(HttpURLConnection)urlcon
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
URL url =new URL(URLName);
HttpURLConnection urlconn = (HttpURLConnection)urlconn .setReques tMethod("G ET");
url.openConnection();
...
HttpURLConnection urlconn = (HttpURLConnection)urlconn
url.openConnection();
...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.