4

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
Community
  • 1
  • 1
jlars62
  • 7,183
  • 7
  • 39
  • 60
  • I'd start by turning on the access log in Tomcat. If you don't see any access at all, then you're almost certainly using a different URL (have you copied from your Java program and pasted into the browser?). – parsifal Jul 02 '13 at 20:08
  • Thanks, yeah I have looked at them, and the only requests getting logged are from the browser. And I have checked the url so many times lol, (cause my thought was that was what it had to be that as well). They are definitely the same. And I am copying and pasting the url value shown in the java debugger just before the request is made. – jlars62 Jul 02 '13 at 20:15
  • maybe you should post a snipplet of your HttpURLConnection implementation, your 'another app' implementation is able to connect to some other url (say google)? – jethroo Jul 02 '13 at 20:46
  • Thanks for the suggestion. I have posted the code and responses in the edit. I am able to connect to other urls such as Google. – jlars62 Jul 02 '13 at 21:02
  • you have to be accurate here please. You wrote `locahost:8080/...`. I ignore the `...`, but you wrote locahost (second 'l' is missing) instead of localhost. Is the mistake only on copying into the question? – yair Jul 02 '13 at 21:26
  • Another question: why don't you use the prefix `http://` for localhost but you do use it for google? – yair Jul 02 '13 at 21:26
  • @yair Sorry both were typos, they are fixed now. – jlars62 Jul 02 '13 at 21:31
  • Something's wrong here, though. On the one hand, you say the request doesn't hit the server. On the other hand, you say you get 404, which means that the server *did* get the request (or *some* server anyway) just didn't find the requried resource. – yair Jul 02 '13 at 21:32
  • Yeah, as weird as it sounds that's exactly what is happening. Thats why this is so weird to me. And the thought did cross my mind that maybe localhost is somehow getting routed to another server. I just dont know enough about that though... – jlars62 Jul 02 '13 at 21:44
  • Could Eclipse somehow intercept the request and return a 404? Cause i am running Tomcat within Eclipse. – jlars62 Jul 02 '13 at 21:52
  • what happens if you just the port to some port that localhost doesn't listen on? Does it return the same response? I currently think it might be something with `HttpUrlConnection` implementation. – yair Jul 02 '13 at 22:07
  • On a bad port I got this: `java.net.ConnectException: Connection refused: connect` – jlars62 Jul 02 '13 at 22:09
  • Another thing. In the post you mentioned there's a suggestion that the server actually returns an error code, only the browser displays the body anyway whereas `HttpUrlConnection.getInputStream()` just returns 404. What does `HttpUrlConnection.getErrorStream()` return? – yair Jul 02 '13 at 22:16
  • Yeah I did look at that and it is just a simple `Not Found` page. I appreciate the assistance btw. – jlars62 Jul 02 '13 at 22:21
  • Also as far as the implementation of `HttpURLConnection`, I dont think that is the problem because I get the same behavior using `org.apache.http.client.methods.HttpGet` class. – jlars62 Jul 02 '13 at 22:24
  • Tell you what, the only place `getInputStream` throws `FileNotFoundException` is when the server has answered `404` (or `410`). So the request defenitely hits *some* server. Let's do another experiment. Please shutdown the Tomcat within eclipse and retry the `HttpUrlConnection` application and also the browser. – yair Jul 02 '13 at 22:44
  • Smart idea. So, I did still get 404 errors. This time from both the browser and the `HttpURLConnection`. So yeah there is some server running somewhere I guess. I closed down tomcat as well as Eclipse. I also tried changing the port, just to double check that I would get a different error, and I did get a different error then when using port 8080. – jlars62 Jul 02 '13 at 23:02
  • I also just tried restarting my computer to end any potential server processes and after a restart same thing - 404s from both. Just weird. I am going to be heading home for tonite so I won't be on here til tomorrow, but Ill definitely be looking into this more tomorrow though. Again, thanks for the help. – jlars62 Jul 02 '13 at 23:11
  • OK so try now `netstat -nao | find "8080"` on Windows or `netstat -nap | grep 8080` on linux and hopefully you'll see the process ID that's stealing your requests. – yair Jul 02 '13 at 23:13
  • since you're gone I thought I'd just put my last comment as an answer... – yair Jul 02 '13 at 23:15

3 Answers3

4

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.

yair
  • 8,945
  • 4
  • 31
  • 50
  • 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 – Telebh Nov 11 '20 at 18:19
  • @Telebh for some reason I can't see what exactly did you get for `netstat`, but since this is a pretty old question, I suggest you start a new question, refer this answer as something you tried, and let the whole Stackoverflow community come for help :) – yair Nov 12 '20 at 09:45
0

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.

0

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

Telebh
  • 305
  • 1
  • 4
  • 13
  • There are a couple potential issues here. Are you sure tomcat is running https ? If it is, I believe it is running on port 443 if `https://127.0.0.1` works. If that is the case, port 8080 will definitely not work, and I dont think `http` will work either. I would recommend running tomcat on port 8080 and just `http` and see if you can get that working first. – jlars62 Nov 11 '20 at 20:26