Solved

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

Posted on 2002-06-10
8
203 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:wky086
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7066567
the http response code must be 404 when the page is not present
0
 

Author Comment

by:wky086
ID: 7067357
Sorry , but which method in java is used to get the error code?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7068044
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:wky086
ID: 7137033
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
 
LVL 9

Expert Comment

by:Venci75
ID: 7137060
URL url =new URL(URLName);
HttpURLConnection urlconn = (HttpURLConnection)urlconn.setRequestMethod("GET");
url.openConnection();
...
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 50 total points
ID: 7137061
regarding the response code - use
urlconn.setResponseCode(); method of the HttpUrlConnection object
0
 
LVL 35

Expert Comment

by:girionis
ID: 8834318
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8838963
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question