Results 1 to 8 of 8

Thread: Check if URL exists (Java)

  1. #1
    Join Date
    Oct 2008
    Posts
    30

    Check if URL exists (Java)

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

    Code:
    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?

  2. #2
    Join Date
    Oct 2008
    Posts
    30

    Re: Check if URL exists (Java)

    I found the method:
    Code:
    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?

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Check if URL exists (Java)

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Check if URL exists (Java)

    Quote Originally Posted by Kelvin Little View Post
    Does anyone else know the solution to this problem?
    Assuming you are using HTTP URLs, Have you tried testing

    Code:
    ((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.

  5. #5
    Join Date
    Oct 2008
    Posts
    30

    Re: Check if URL exists (Java)

    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.

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Check if URL exists (Java)

    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.

  7. #7
    Join Date
    Oct 2008
    Posts
    30

    Re: Check if URL exists (Java)

    Thanks for the info.

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

  8. #8
    Join Date
    Jan 2010
    Posts
    1

    Re: Check if URL exists (Java)

    Hiiii, following code is part of my pgm,will u plz tell me why its giving malformedurlexception no protocol error... plzz


    for (i =nItems; i>=0;i--) // start at end,
    {
    String s=queArray[i];
    URL hp1=new URL(s);
    URLConnection hpcon1=hp1.openConnection();
    int len1=hpcon1.getContentLength();
    System.out.println("Welcome"+len+" "+len1);
    if (len<len1) // if new item larger,
    queArray[i + 1] = queArray[i]; // shift upward
    else
    // if smaller,
    break; // done shifting
    }

Similar Threads

  1. How to check if file exists in directory with Php
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 12:36 PM
  2. How to check if variable exists in C#
    By Hamlet in forum Software Development
    Replies: 3
    Last Post: 28-08-2009, 07:30 PM
  3. Check if database or table exists
    By Xan in forum Software Development
    Replies: 2
    Last Post: 29-05-2009, 04:53 PM
  4. Powershell: To check a directory exists or not?
    By Chandrakant81 in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 06:26 PM
  5. How to Check File Exists or Not in Linux
    By Ettan in forum Software Development
    Replies: 0
    Last Post: 19-12-2008, 01:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,222,750.66455 seconds with 17 queries