Podge
Podge

Reputation: 236

Check if URL is valid or exists in java

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?

Upvotes: 5

Views: 19758

Answers (5)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

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

Upvotes: 3

sealz
sealz

Reputation: 5408

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
     } 
}

Upvotes: 11

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8411

  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.

Upvotes: 5

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

HttpURLConnection javadoc

Upvotes: 1

jarnbjo
jarnbjo

Reputation: 34323

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

Upvotes: 1

Related Questions