问题
I am getting the weirdest Tomcat error. I have a web app running on my localhost at port 8080 using tomcat and everything appears to running great, no errors etc. However, when I try to access this web app from another app using the HttpURLConnection class, I am getting a 404 error. The weird part is, when I put the same URL in the browser it returns a 200 code and has a valid response.
I have tried/checked the following from these posts : post1 and post2
- Setting the
User Agent
andAccept
headers. - I have checked the response body (using
HttpURLConnection.getInputStream()
as well asHttpURLConnection.getErrorStream()
, in the case that 404 was an improper return code) and am indeed getting a page not found response. - I have tried setting the
connection.setDoOutput()
totrue
andfalse
but it has not helped. - Tried changing
localhost
to127.0.0.1
.
Some more information, I have looked at the Tomcat
access logs, and it appears that the request is never hitting the server (meaning the request never gets logged). However, when I put the url in the browser (and get a valid response), the request does show up in the logs.
One more thing, I am running tomcat using eclipse. And yes the app is being deployed on the server.
Also, I have found someone that appears to have had the exact same problem here, but there is no solution, so I am bringing the question to the great community of SO!
EDIT: Code from calling app:
For privacy reasons, I have kept the url hidden
public static void main(String[] args) {
final String url = "";
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(false);
System.out.println(con.getResponseCode());
System.out.println(getStringFromInputStream(con.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DONE");
}
Yes, this does work for other hosts such as google. The response I get from http://www.google.com
is this:
200
<!doctype html><html....>*bunch of html*</html>
DONE
Reply for http://localhost:8080/...
:
404
java.io.FileNotFoundException: *url*
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at Get.main(Get.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.io.FileNotFoundException: *url*
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1623)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at Get.main(Get.java:31)
... 5 more
DONE
回答1:
So, by shutting down the Tomcat that runs from Eclipse and retrying the application and browser requests, we've figured out that some process is stealing your requests.
You can find the thief by running netstat -nao | find "8080"
on Windows or netstat -nap | grep 8080
on Linux. It should show a line with LISTENING
and 127.0.0.1:8080
and next would be the process ID.
回答2:
i meet the same error, and got the reason, the username in jdbc configure was bind to access mysql with network address,ie:192.168.. instead of localhost, i change the username and successed.
来源:https://stackoverflow.com/questions/17433906/404-when-accessing-tomcat-with-httpurlconnection-200-from-browser