Home > Common Problem > body text

How to get http status code in java

zbt
Release: 2023-10-07 14:49:30
Original
1344 people have browsed it

The http status code can be obtained by using the HttpURLConnection class. The specific operations are as follows: 1. Create a URL object; 2. Use the object to open a connection; 3. Set the request method to GET; 4. Use the `getResponseCode()` method to obtain the HTTP status code; 5. Print Get the status code and close the connection.

How to get http status code in java

Operating system for this tutorial: windows10 system, Java17, DELL G3 computer.

Java is a programming language widely used in network programming. It provides many classes and methods for handling HTTP requests and responses. In network communication, the HTTP status code is an identifier of the server's response to the request, which indicates the processing result of the request. This article will introduce how to get HTTP status code using Java.

Java provides many classes and methods to send HTTP requests and process HTTP responses, the most commonly used of which are the HttpURLConnection class and the java.net.HttpURLConnection class in the java.net package. Here is a simple example that demonstrates how to get the HTTP status code using Java:

import java.net.HttpURLConnection;
import java.net.URL;
public class HttpStatusExample {
public static void main(String[] args) {
try {
// 创建URL对象
URL url = new URL("http://www.example.com");
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法
connection.setRequestMethod("GET");
// 获取响应码
int responseCode = connection.getResponseCode();
// 打印响应码
System.out.println("HTTP状态码: " + responseCode);
// 关闭连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Copy after login

In the above example, we first created a URL object and opened a connection using that object. Then, we set the request method to GET and used the `getResponseCode()` method to obtain the HTTP status code. Finally, we print the obtained status code and close the connection.

In addition to using the `getResponseCode()` method, we can also use the `getResponseMessage()` method to get the status message of the HTTP response. For example, `connection.getResponseMessage()` will return a string containing the status message corresponding to the status code.

It should be noted that the code in the above example may throw an exception, so we use a try-catch block to catch and handle the exception. In practical applications, we should handle exceptions according to specific situations.

In summary, Java provides simple and powerful tools to handle HTTP requests and responses. By using the HttpURLConnection class, we can easily get the HTTP status code and process it further as needed. Whether you are building a web crawler, developing a web application, or making API calls, it is important to understand how to obtain HTTP status codes .

The above is the detailed content of How to get http status code in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template