简体   繁体   中英

HttpURLConnection respond 404 when file clearly exist

I have a java program that is trying to read any arbitrary file from URL. However, it return an 404 error when the file clearly exist, try it for yourself with the URL. What is wrong?

      URL url = new URL("http://images.all-free-download.com/images/graphiclarge/blue_abstract_background_310971.jpg");
      HttpURLConnection myHTTPConTest = null;
      myHTTPConTest = (HttpURLConnection) url.openConnection();
      int responseCode = myHTTPConTest.getResponseCode();  // Returns 404

Added user-agent, no change:

      myHTTPConTest.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401"); 

I think you had a bad luck and you just hit the server restart time or something like that. Because it returns 200 now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM