Java - Quickest way to check if URL exists

后端 未结 3 559
一个人的身影
一个人的身影 2021-01-03 21:49

Hi I am writing a program that goes through many different URLs and just checks if they exist or not. I am basically checking if the error code returned is 404 or not. Howev

3条回答
  •  难免孤独
    2021-01-03 22:28

    Try sending a "HEAD" request instead of get request. That should be faster since the response body is not downloaded.

    huc.setRequestMethod("HEAD");
    

    Again instead of checking if response status is not 400, check if it is 200. That is check for positive instead of negative. 404,403,402.. all 40x statuses are nearly equivalent to invalid non-existant url.

    You may make use of multi-threading to make it even faster.

提交回复
热议问题