PDA

View Full Version : How to find the valid url only


kim Seoul
Jul 17, 2013, 11:17 AM
Hello guys

I have a project to check out thousands of urls , the problem I'm facing now
in this code is that I can't handle the out put

I only need to get message if the url is exists , otherwise I do not want to get any message ;

her is what I get :
Response Message OK // if the url is found
and
Response Message: Not Found // if the url is not found

I need to get message only and only if url is found , if it not found I do not wan to
get any message!

how to edit down source code in order to make only show message if url is exist and show no thing if url is not exist .
thank you in advance .


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


public class URLUtils {

public static void main(String[] args) {

URLUtils.checkIfURLExists("https://www.google.co.kr/");





}

public static boolean checkIfURLExists(String targetUrl) {
HttpURLConnection httpUrlConn;
try {

httpUrlConn = (HttpURLConnection) new URL(targetUrl)
.openConnection();



httpUrlConn.setConnectTimeout(30000);
httpUrlConn.setReadTimeout(30000);


System.out.println("Response Message: "
+ httpUrlConn.getResponseMessage());

return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);



}
catch (Exception e) {


return false;
}
}

}