Java HTTP getResponseCode returns 200 for non-existent URL

后端 未结 5 371
长情又很酷
长情又很酷 2021-01-25 03:04

I was expecting this code to return a 404, however it produces the output :

\"Response code is 200\"

Would it be possible to learn how to differentiate between e

5条回答
  •  爱一瞬间的悲伤
    2021-01-25 03:47

    EDIT: I see you've call openConnection() but not connect() - could that be the problem? I would expect getResponseCode() to actually make the request if it hasn't already, but it's worth just trying that...


    That suggests you've possible got some DNS resolver which redirects to a "helper" (spam) page, or something like that.

    The easiest way to see exactly what's going on here is to use Wireshark - have that up and capturing traffic (HTTP-only, to make life easier) and then run your code. You should be able to see what's going on that way.

    Note that I wouldn't have expected a 404 - because that would involve being able to find a web server to talk to to start with. If you're trying to go to a host which doesn't involve, there shouldn't be an HTTP response at all. I'd expect connect() to throw an exception.

提交回复
热议问题