Check if URL is valid or exists in java

18,252

Solution 1

You could just use httpURLConnection. If it is not valid you won't get anything back.

    HttpURLConnection connection = null;
try{         
    URL myurl = new URL("http://www.myURL.com");        
    connection = (HttpURLConnection) myurl.openConnection(); 
    //Set request to header to reduce load as Subirkumarsao said.       
    connection.setRequestMethod("HEAD");         
    int code = connection.getResponseCode();        
    System.out.println("" + code); 
} catch {
//Handle invalid URL
}

Or you could ping it like you would from CMD and record the response.

String myurl = "google.com"
String ping = "ping " + myurl 

try {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(ping);
    r.exec(ping);    
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String inLine;
    BufferedWriter write = new BufferedWriter(new FileWriter("C:\\myfile.txt"));

    while ((inLine = in.readLine()) != null) {             
            write.write(inLine);   
            write.newLine();
        }
            write.flush();
            write.close();
            in.close();              
     } catch (Exception ex) {
       //Code here for what you want to do with invalid URLs
     } 
}

Solution 2

  1. A malformed url will give you an exception.
  2. To know if you the url is active or not you have to hit the url. There is no other way.

You can reduce the load by requesting for a header from the url.

Solution 3

package com.my;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

public class StrTest {

public static void main(String[] args) throws IOException {

    try {
        URL url = new URL("http://www.yaoo.coi");
        InputStream i = null;

        try {
            i = url.openStream();
        } catch (UnknownHostException ex) {
            System.out.println("THIS URL IS NOT VALID");
        }

        if (i != null) {
            System.out.println("Its working");
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
           }
      }
  }

output : THIS URL IS NOT VALID

Solution 4

Open a connection and check if the response contains valid XML? Was that too obvious or do you look for some other magic?

Solution 5

You may want to use HttpURLConnection and check for error status:

HttpURLConnection javadoc

Share:
18,252
Podge
Author by

Podge

Updated on June 05, 2022

Comments

  • Podge
    Podge almost 2 years

    I'm writing a Java program which hits a list of urls and needs to first know if the url exists. I don't know how to go about this and cant find the java code to use.

    The URL is like this:

    http: //ip:port/URI?Attribute=x&attribute2=y
    

    These are URLs on our internal network that would return an XML if valid.

    Can anyone suggest some code?