Java – the URL can be accessed using a browser, but it is still a FileNotFoundException with urlconnection
I use httpurlconnection to connect to a website and receive a response code = 404 (http_not_found) However, there is no problem opening the website in my browser (ie)
Why is it different? What can I do?
Greetings, Papan
This is my show
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class TestGet { private static URL source; public static void main(String[] args) { doGet(); } public static void doGet() { try { source = new URL("http://localhost:8080/"); System.out.println("Url is" + source.toString()); URLConnection connection = source.openConnection(); connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) "); connection.setRequestProperty("Accept","*/*"); connection.setDoInput(true); connection.setDoOutput(true); System.out.println(((HttpURLConnection) connection).getResponseCode()); BufferedReader rdr = new BufferedReader(new InputStreamReader( connection.getInputStream())); StringBuffer b = new StringBuffer(); String line = null; while (true) { line = rdr.readLine(); if (line == null) break; b.append(line); } } catch (Exception e) { e.printStackTrace(); System.err.println(e.toString()); } } }
stack trace
Url ishttp://localhost:8080/ 404 java.io.FileNotFoundException: http://localhost:8080/ at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(UnkNown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(UnkNown Source) at java.lang.reflect.Constructor.newInstance(UnkNown Source) at sun.net.www.protocol.http.HttpURLConnection$6.run(UnkNown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(UnkNown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(UnkNown Source) at TestGet.doGet(TestGet.java:28) at TestGet.main(TestGet.java:11) Caused by: java.io.FileNotFoundException: http://localhost:8080/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(UnkNown Source) at java.net.HttpURLConnection.getResponseCode(UnkNown Source) at TestGet.doGet(TestGet.java:26) ... 1 more java.io.FileNotFoundException: http://localhost:8080/
Solution
Your 404 error means that the requested response was not found First, you need to ensure that the server is provided on http: / / localhost: 8080 /, and you must return something in code 200 If not, we can't help you
The easiest way to test a web address for anything is to paste it into your web browser's address bar and click However, this does not guarantee that Java code can access it For example, if the server is designed to respond to 404, if it cannot find the web browser user agent header
Since the server returns a status code of 200 or 404, this is not a firewall problem
According to the problem with your latest version, you can view it using a web browser, but you can't download it using your Java code, and the title seems to be set correctly I can only see two questions:
>You should not set connection setDoOutput(true); True, this will force the connection to perform HTTP post instead of get, and the server may not support post. > Your server may always return 404, even if it should be 200 Because the web browser doesn't care about the error state and tries to render all the content, it seems to work from the web browser If so, you should first fix the server to respond correctly, otherwise try to get the error stream instead of httpurlconnection #geterrorstream()