How to quickly check if URL server is available

后端 未结 8 2031
执念已碎
执念已碎 2020-12-09 16:30

I have a URL in the form

http://www.mywebsite.com/util/conv?a=1&from=%s&to=%s

And want to check if it is available.

The lin

相关标签:
8条回答
  • 2020-12-09 16:59

    As mentioned by 'eridal', that should be faster, open the socket; however, it only tells you that a server is listening on the host and port but to be sure, you need to write HTTP or alternately a bad request (junk), you should get HTTP/1.1 400 Bad Request. By reading the first line returned, if it contains HTTP, then you are sure that the server is a HTTP server. This way you are sure that the server is available as well as a HTTP server.

    This is in extension to the above answer by eridal.

    0 讨论(0)
  • 2020-12-09 17:02

    Below code waits until webpage is available:

    public void waitForWebavailability() throws Exception {
            boolean success = false;
            long waitTime = 0;
            while (!success) {
                try {
                    waitTest(30000);
                    waitTime += 30000;
                    if (waitTime > 600000) {
    
                        System.out.println("Web page is not available");
                    }
                    webDriver.get("http://www.google.com");
                    if (webDriver.getTitle().toLowerCase().contains("sometitle")) {
                        success = true;
                    }
                } catch (Exception e) {
                    success = false;
                }
    
            }
    
        }
    
    // Code related to waittest
    
    public void waitTest(long millis) throws Exception {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                throw new Exception(e);
            }
        }
    
    0 讨论(0)
提交回复
热议问题