There is a requirement in the project that is like this. You need to send a url request through Java to check whether the url is valid. At this time, we can judge by obtaining the status code.
try {URL u = new URL("http://10.1.2.8:8080/fqz/page/qizha/pros_add.jsp");try {HttpURLConnection uConnection = (HttpURLConnection) u.openConnection();try {uConnection.connect();System.out.println(uConnection.getResponseCode());connect = true;InputStream is = uConnection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));StringBuilder sb = new StringBuilder();while(br.read() != -1){sb.append(br.readLine());}String content = new String(sb);content = new String(content.getBytes("GBK"), "ISO-8859-1");System.out.println(content);br.close();} catch (Exception e) {connect = false;e.printStackTrace();System.out.println("connect failed");}} catch (IOException e) {System.out.println("build failed");e.printStackTrace();}} catch (MalformedURLException e) {System.out.println("build url failed");e.printStackTrace();}The status code can be obtained through uConnection.getResponseCode(). Then determine whether the website exists. The following is the meaning represented by different status codes.
http status return code 1xx (temporary response)
A status code that represents a temporary response and requires the requester to continue the operation.
http status return code description
100 (Continue) The requester shall continue to make the request. The server returns this code to indicate that the first part of the request has been received and is waiting for the rest.
101 (Switch Protocol) The requester has asked the server to switch the protocol, and the server has confirmed and is ready to switch.
http status return code 2xx (success)
The status code indicating that the request was successfully processed.
http status return code description
200 (Successful) The server has successfully processed the request. Typically, this means that the server provides the requested webpage.
201 (Created) The request was successful and the server created a new resource.
202 (Accepted) The server has accepted the request, but has not been processed yet.
203 (Unauthorized Information) The server has successfully processed the request, but the returned information may come from another source.
204 (No content) The server successfully processed the request, but did not return any content.
205 (Reset Content) The server successfully processed the request, but did not return anything.
206 (Some content) The server successfully processed some GET requests.
http status return code 3xx (redirect)
It means that further operations are needed to complete the request. Typically, these status codes are used to redirect.
http status return code description
300 (multiple choices) The server can perform a variety of operations for requests. The server can select an operation based on the user agent, or provide a list of operations for the requester to select.
301 (Permanent Move) The requested web page has been moved to the new location permanently. When the server returns this response (response to a GET or HEAD request), the requestor is automatically transferred to the new location.
302 (Temporary Move) The server currently responds to the request from a web page in a different location, but the requester should continue to use the original location to make future requests.
303 (View other locations) The server returns this code when the requester should use a separate GET request for different locations to retrieve the response.
304 (Unmodified) Since the last request, the requested web page has not been modified. When the server returns this response, the content of the web page will not be returned.
305 (Use a proxy) The requester can only use the proxy to access the requested web page. If the server returns this response, it also means that the requester should use a proxy.
307 (Temporary Redirection) The server currently responds to the request from a web page in a different location, but the requester should continue to use the original location to make future requests.
http status return code 4xx (request error)
These status codes indicate that a request may have an error, hindering the server's processing.
http status return code description
400 (Bad Request) The server does not understand the requested syntax.
401 (Unauthorized) Request for authentication. The server may return this response for web pages that need to be logged in.
403 (Prohibited) The server rejects the request.
404 (Not Found) The server cannot find the requested web page.
405 (Method Disable) Disable the method specified in the request.
406 (Not accepted) A web page that cannot respond to the requested content feature.
407 (Proxy Authorization Required) This status code is similar to 401 (unauthorized), but specifies that the requester should authorize the use of the proxy.
408 (Request timeout) A timeout occurred while the server was waiting for the request.
409 (Conflict) A conflict occurred when the server completed the request. The server must include information about the conflict in the response.
410 (Deleted) If the requested resource has been permanently deleted, the server returns this response.
411 (Valid length required) The server does not accept requests that do not contain the valid content length header field.
412 (Prerequisite not met) The server does not meet one of the prerequisites set by the requester in the request.
413 (The request entity is too large) The server cannot process the request because the request entity is too large, exceeding the server's processing capacity.
414 (The requested URI is too long) The requested URI (usually the URL) is too long and the server cannot handle it.
415 (Unsupported Media Type) The requested format is not supported by the requested page.
416 (The request scope does not meet the requirements) If the page cannot provide the requested scope, the server returns this status code.
417 (Expected value not met) The server does not meet the requirements of the "Expected" request header field.
http status return code 5xx (server error)
These status codes indicate an internal error occurred on the server when trying to process the request. These errors may be errors in the server itself, not request errors.
http status return code description
500 (Internal Error of Server) The server encountered an error and could not complete the request.
501 (Not implemented yet) The server does not have the function to complete the request. For example, this code may be returned when the server fails to recognize the request method.
502 (Error Gateway) The server, as a gateway or proxy, receives an invalid response from the upstream server.
503 (Service Not Available) The server is currently unavailable (due to overload or downtime maintenance). Usually, this is just a temporary state.
504 (Gateway timeout) The server acts as a gateway or proxy, but does not receive the request from the upstream server in time.
505 (HTTP version is not supported) The server does not support the HTTP protocol version used in the request.
Some common http status return codes are:
200 - The server returns to the web page successfully
404 - The requested web page does not exist
503 - Service not available
The above simple example of Java sending http request and obtaining status code is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.