How to quickly test if a URL exists and has content in java?










0















I am looking to test to see if hundreds of URLs exist, and the current way I have takes too much time. This is what I have found so far:



public static boolean checkURL(URL u)

HttpURLConnection connection = null;
try

connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
System.out.println("" + code);
// You can determine on HTTP return code received. 200 is success.
if (code == 200)

return true;

else

return false;


catch (MalformedURLException e)

// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("error");

catch (IOException e)

System.out.println("error2");
// TODO Auto-generated catch block
// e.printStackTrace();

finally

if (connection != null)

connection.disconnect();



return false;



Although this does successfully find whether a URL exists and has content, it does so in a lengthy period of time, with the program often taking upwards of five minutes to execute. Does anyone know more efficient ways to test this?



Note: It is important to test that not only the url returns 200, but also that the website doesn't timeout.










share|improve this question






















  • How can you test that a website doesn't time out without waiting for it to time out?

    – shmosel
    Nov 14 '18 at 1:10











  • It seems to me that pinging takes a lot less time than this method, although I'm unsure on how to execute that

    – Alex Reed
    Nov 14 '18 at 1:20











  • Did you try searching?

    – shmosel
    Nov 14 '18 at 1:21











  • I did, when I found questions about pinging websites and tried the solutions, the program still returned sites that timed out.

    – Alex Reed
    Nov 14 '18 at 1:32















0















I am looking to test to see if hundreds of URLs exist, and the current way I have takes too much time. This is what I have found so far:



public static boolean checkURL(URL u)

HttpURLConnection connection = null;
try

connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
System.out.println("" + code);
// You can determine on HTTP return code received. 200 is success.
if (code == 200)

return true;

else

return false;


catch (MalformedURLException e)

// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("error");

catch (IOException e)

System.out.println("error2");
// TODO Auto-generated catch block
// e.printStackTrace();

finally

if (connection != null)

connection.disconnect();



return false;



Although this does successfully find whether a URL exists and has content, it does so in a lengthy period of time, with the program often taking upwards of five minutes to execute. Does anyone know more efficient ways to test this?



Note: It is important to test that not only the url returns 200, but also that the website doesn't timeout.










share|improve this question






















  • How can you test that a website doesn't time out without waiting for it to time out?

    – shmosel
    Nov 14 '18 at 1:10











  • It seems to me that pinging takes a lot less time than this method, although I'm unsure on how to execute that

    – Alex Reed
    Nov 14 '18 at 1:20











  • Did you try searching?

    – shmosel
    Nov 14 '18 at 1:21











  • I did, when I found questions about pinging websites and tried the solutions, the program still returned sites that timed out.

    – Alex Reed
    Nov 14 '18 at 1:32













0












0








0








I am looking to test to see if hundreds of URLs exist, and the current way I have takes too much time. This is what I have found so far:



public static boolean checkURL(URL u)

HttpURLConnection connection = null;
try

connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
System.out.println("" + code);
// You can determine on HTTP return code received. 200 is success.
if (code == 200)

return true;

else

return false;


catch (MalformedURLException e)

// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("error");

catch (IOException e)

System.out.println("error2");
// TODO Auto-generated catch block
// e.printStackTrace();

finally

if (connection != null)

connection.disconnect();



return false;



Although this does successfully find whether a URL exists and has content, it does so in a lengthy period of time, with the program often taking upwards of five minutes to execute. Does anyone know more efficient ways to test this?



Note: It is important to test that not only the url returns 200, but also that the website doesn't timeout.










share|improve this question














I am looking to test to see if hundreds of URLs exist, and the current way I have takes too much time. This is what I have found so far:



public static boolean checkURL(URL u)

HttpURLConnection connection = null;
try

connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
System.out.println("" + code);
// You can determine on HTTP return code received. 200 is success.
if (code == 200)

return true;

else

return false;


catch (MalformedURLException e)

// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("error");

catch (IOException e)

System.out.println("error2");
// TODO Auto-generated catch block
// e.printStackTrace();

finally

if (connection != null)

connection.disconnect();



return false;



Although this does successfully find whether a URL exists and has content, it does so in a lengthy period of time, with the program often taking upwards of five minutes to execute. Does anyone know more efficient ways to test this?



Note: It is important to test that not only the url returns 200, but also that the website doesn't timeout.







java performance url timeout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:08









Alex ReedAlex Reed

325




325












  • How can you test that a website doesn't time out without waiting for it to time out?

    – shmosel
    Nov 14 '18 at 1:10











  • It seems to me that pinging takes a lot less time than this method, although I'm unsure on how to execute that

    – Alex Reed
    Nov 14 '18 at 1:20











  • Did you try searching?

    – shmosel
    Nov 14 '18 at 1:21











  • I did, when I found questions about pinging websites and tried the solutions, the program still returned sites that timed out.

    – Alex Reed
    Nov 14 '18 at 1:32

















  • How can you test that a website doesn't time out without waiting for it to time out?

    – shmosel
    Nov 14 '18 at 1:10











  • It seems to me that pinging takes a lot less time than this method, although I'm unsure on how to execute that

    – Alex Reed
    Nov 14 '18 at 1:20











  • Did you try searching?

    – shmosel
    Nov 14 '18 at 1:21











  • I did, when I found questions about pinging websites and tried the solutions, the program still returned sites that timed out.

    – Alex Reed
    Nov 14 '18 at 1:32
















How can you test that a website doesn't time out without waiting for it to time out?

– shmosel
Nov 14 '18 at 1:10





How can you test that a website doesn't time out without waiting for it to time out?

– shmosel
Nov 14 '18 at 1:10













It seems to me that pinging takes a lot less time than this method, although I'm unsure on how to execute that

– Alex Reed
Nov 14 '18 at 1:20





It seems to me that pinging takes a lot less time than this method, although I'm unsure on how to execute that

– Alex Reed
Nov 14 '18 at 1:20













Did you try searching?

– shmosel
Nov 14 '18 at 1:21





Did you try searching?

– shmosel
Nov 14 '18 at 1:21













I did, when I found questions about pinging websites and tried the solutions, the program still returned sites that timed out.

– Alex Reed
Nov 14 '18 at 1:32





I did, when I found questions about pinging websites and tried the solutions, the program still returned sites that timed out.

– Alex Reed
Nov 14 '18 at 1:32












1 Answer
1






active

oldest

votes


















0














Your code looks good and it should be the easiest way to check for url. You might want to add a timeout in the HttpURLConnection.



Sample code for reference.



enter code here
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlChecker
public static void main(String args)
System.out.println(URLExists("http://slowwly.robertomurray.co.uk/delay/
3000/url/http://www.google.co.uk"));


public static boolean URLExists(String targetUrl)
HttpURLConnection urlConnection;
try
urlConnection = (HttpURLConnection) new
URL(targetUrl).openConnection();
urlConnection.setRequestMethod("HEAD");
// Set timeouts 2000 in milliseconds and throw exception
urlConnection.setConnectTimeout(2000);
urlConnection.setReadTimeout(2000);
/* Set timeouts 4000 in milliseconds and it should work as the url
should return back in 3 seconds.
httpUrlConn.setConnectTimeout(4000);
httpUrlConn.setReadTimeout(4000);
*/
System.out.println("Response Code =>"+
urlConnection.getResponseCode());
System.out.println("Response Msg =>"+
urlConnection.getResponseMessage());
return (urlConnection.getResponseCode() ==
HttpURLConnection.HTTP_OK);
catch (Exception e)
System.out.println("Exception => " + e.getMessage());
return false;








share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291755%2fhow-to-quickly-test-if-a-url-exists-and-has-content-in-java%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your code looks good and it should be the easiest way to check for url. You might want to add a timeout in the HttpURLConnection.



    Sample code for reference.



    enter code here
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class UrlChecker
    public static void main(String args)
    System.out.println(URLExists("http://slowwly.robertomurray.co.uk/delay/
    3000/url/http://www.google.co.uk"));


    public static boolean URLExists(String targetUrl)
    HttpURLConnection urlConnection;
    try
    urlConnection = (HttpURLConnection) new
    URL(targetUrl).openConnection();
    urlConnection.setRequestMethod("HEAD");
    // Set timeouts 2000 in milliseconds and throw exception
    urlConnection.setConnectTimeout(2000);
    urlConnection.setReadTimeout(2000);
    /* Set timeouts 4000 in milliseconds and it should work as the url
    should return back in 3 seconds.
    httpUrlConn.setConnectTimeout(4000);
    httpUrlConn.setReadTimeout(4000);
    */
    System.out.println("Response Code =>"+
    urlConnection.getResponseCode());
    System.out.println("Response Msg =>"+
    urlConnection.getResponseMessage());
    return (urlConnection.getResponseCode() ==
    HttpURLConnection.HTTP_OK);
    catch (Exception e)
    System.out.println("Exception => " + e.getMessage());
    return false;








    share|improve this answer



























      0














      Your code looks good and it should be the easiest way to check for url. You might want to add a timeout in the HttpURLConnection.



      Sample code for reference.



      enter code here
      import java.net.HttpURLConnection;
      import java.net.URL;

      public class UrlChecker
      public static void main(String args)
      System.out.println(URLExists("http://slowwly.robertomurray.co.uk/delay/
      3000/url/http://www.google.co.uk"));


      public static boolean URLExists(String targetUrl)
      HttpURLConnection urlConnection;
      try
      urlConnection = (HttpURLConnection) new
      URL(targetUrl).openConnection();
      urlConnection.setRequestMethod("HEAD");
      // Set timeouts 2000 in milliseconds and throw exception
      urlConnection.setConnectTimeout(2000);
      urlConnection.setReadTimeout(2000);
      /* Set timeouts 4000 in milliseconds and it should work as the url
      should return back in 3 seconds.
      httpUrlConn.setConnectTimeout(4000);
      httpUrlConn.setReadTimeout(4000);
      */
      System.out.println("Response Code =>"+
      urlConnection.getResponseCode());
      System.out.println("Response Msg =>"+
      urlConnection.getResponseMessage());
      return (urlConnection.getResponseCode() ==
      HttpURLConnection.HTTP_OK);
      catch (Exception e)
      System.out.println("Exception => " + e.getMessage());
      return false;








      share|improve this answer

























        0












        0








        0







        Your code looks good and it should be the easiest way to check for url. You might want to add a timeout in the HttpURLConnection.



        Sample code for reference.



        enter code here
        import java.net.HttpURLConnection;
        import java.net.URL;

        public class UrlChecker
        public static void main(String args)
        System.out.println(URLExists("http://slowwly.robertomurray.co.uk/delay/
        3000/url/http://www.google.co.uk"));


        public static boolean URLExists(String targetUrl)
        HttpURLConnection urlConnection;
        try
        urlConnection = (HttpURLConnection) new
        URL(targetUrl).openConnection();
        urlConnection.setRequestMethod("HEAD");
        // Set timeouts 2000 in milliseconds and throw exception
        urlConnection.setConnectTimeout(2000);
        urlConnection.setReadTimeout(2000);
        /* Set timeouts 4000 in milliseconds and it should work as the url
        should return back in 3 seconds.
        httpUrlConn.setConnectTimeout(4000);
        httpUrlConn.setReadTimeout(4000);
        */
        System.out.println("Response Code =>"+
        urlConnection.getResponseCode());
        System.out.println("Response Msg =>"+
        urlConnection.getResponseMessage());
        return (urlConnection.getResponseCode() ==
        HttpURLConnection.HTTP_OK);
        catch (Exception e)
        System.out.println("Exception => " + e.getMessage());
        return false;








        share|improve this answer













        Your code looks good and it should be the easiest way to check for url. You might want to add a timeout in the HttpURLConnection.



        Sample code for reference.



        enter code here
        import java.net.HttpURLConnection;
        import java.net.URL;

        public class UrlChecker
        public static void main(String args)
        System.out.println(URLExists("http://slowwly.robertomurray.co.uk/delay/
        3000/url/http://www.google.co.uk"));


        public static boolean URLExists(String targetUrl)
        HttpURLConnection urlConnection;
        try
        urlConnection = (HttpURLConnection) new
        URL(targetUrl).openConnection();
        urlConnection.setRequestMethod("HEAD");
        // Set timeouts 2000 in milliseconds and throw exception
        urlConnection.setConnectTimeout(2000);
        urlConnection.setReadTimeout(2000);
        /* Set timeouts 4000 in milliseconds and it should work as the url
        should return back in 3 seconds.
        httpUrlConn.setConnectTimeout(4000);
        httpUrlConn.setReadTimeout(4000);
        */
        System.out.println("Response Code =>"+
        urlConnection.getResponseCode());
        System.out.println("Response Msg =>"+
        urlConnection.getResponseMessage());
        return (urlConnection.getResponseCode() ==
        HttpURLConnection.HTTP_OK);
        catch (Exception e)
        System.out.println("Exception => " + e.getMessage());
        return false;









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 2:57









        SK -SK -

        796




        796





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291755%2fhow-to-quickly-test-if-a-url-exists-and-has-content-in-java%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo