如何建立到localhost:8080的URL连接并检查HTTP响应代码是否介于200和209之间?(How to make a URL connection to localhost:8080 and check if the HTTP Response code is between 200 and 209? [closed])

我一直在努力为这个问题找到答案。

如何建立到localhost:8080的URL连接并检查HTTP响应代码是否介于200和209之间?

I have been struggling to get answer for this question.

How to make a URL connection to localhost:8080 and check if the HTTP Response code is between 200 and 209?

最满意答案

这应该这样做:

URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int code = connection.getResponseCode(); if (code>=200 && code <= 209){ //... }

This should do it:

URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int code = connection.getResponseCode(); if (code>=200 && code <= 209){ //... }如何建立到localhost:8080的URL连接并检查HTTP响应代码是否介于200和209之间?(How to make a URL connection to localhost:8080 and check if the HTTP Response code is between 200 and 209? [closed])

我一直在努力为这个问题找到答案。

如何建立到localhost:8080的URL连接并检查HTTP响应代码是否介于200和209之间?

I have been struggling to get answer for this question.

How to make a URL connection to localhost:8080 and check if the HTTP Response code is between 200 and 209?

最满意答案

这应该这样做:

URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int code = connection.getResponseCode(); if (code>=200 && code <= 209){ //... }

This should do it:

URL url = new URL("http://example.com"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int code = connection.getResponseCode(); if (code>=200 && code <= 209){ //... }