Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if URL is valid or exists in java

Tags:

java

http

url

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?

like image 934
Podge Avatar asked May 11 '12 12:05

Podge


2 Answers

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
     } 
}
like image 180
sealz Avatar answered Oct 05 '22 07:10

sealz


  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.

like image 28
Subir Kumar Sao Avatar answered Oct 05 '22 06:10

Subir Kumar Sao