I need a monitor class that regularly checks whether a given HTTP URL is available. I can take care of the “regularly” part using the Spring TaskExecutor abstraction, so that’s not the topic here. The question is: What is the preferred way to ping a URL in java? Here is my current code as a starting point: Is this any
Tag: url
HttpURLConnection timeout settings
I want to return false if the URL takes more then 5 seconds to connect – how is this possible using Java? Here is the code I am using to check if the URL is valid Answer HttpURLConnection has a setConnectTimeout method. Just set the timeout to 5000 milliseconds, and then catch java.net.SocketTimeoutException Your code should look something like this:
Mocking a URL in Java
We have a URL object in one of our Java classes that we want to mock, but it’s a final class so we cannot. We do not want to go a level above, and mock the InputStream because that will still leave us with untested code (we have draconian test coverage standards). I’ve tried jMockIt’s reflective powers but we work