Solved

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

Posted on 2002-06-10
8
200 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
Comment Utility
the http response code must be 404 when the page is not present
0
 

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
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
 

Author Comment

by:wky086
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 9

Expert Comment

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

Accepted Solution

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

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 54
powerN  challenge 3 46
eclipse java  build path 6 16
how do i compare an object based on two fields 6 26
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now