简体   繁体   中英

404 when accessing Tomcat with HttpUrlConnection, 200 from browser

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

  1. Setting the User Agent and Accept headers.
  2. I have checked the response body (using HttpURLConnection.getInputStream() as well as HttpURLConnection.getErrorStream() , in the case that 404 was an improper return code) and am indeed getting a page not found response.
  3. I have tried setting the connection.setDoOutput() to true and false but it has not helped.
  4. Tried changing localhost to 127.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

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" netstat -nao | find "8080" on Windows or netstat -nap | grep 8080 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.

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.

I have the same exact issue , when I run https://127.0.0.1 from browser it works fine, but https://localhost or http://localhost:8080 in browser and Eclispe does not work. I have just tried netstat -nao|find "8080" on windows, I got the following


TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       30748
  TCP    25.208.159.83:64002    165.225.39.27:8080     TIME_WAIT       0
  TCP    25.208.159.83:64037    165.225.39.27:8080     TIME_WAIT       0
  TCP    25.208.159.83:64117    165.225.39.27:8080     TIME_WAIT       0
  TCP    25.208.159.83:64197    185.46.212.88:8080     SYN_SENT        11044
  TCP    25.208.159.83:64200    165.225.39.27:8080     TIME_WAIT       0
  TCP    [::]:8080              [::]:0                 LISTENING       30748

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