Home >Java >javaTutorial >Guide to HTTP Codes in Java: How to understand what the server wants from you?
In the world of HTTP codes, every server response is like a message from the other end of the universe. You sent a request, and now this mysterious code appears on the screen. What does it mean? How should we understand it? Let's figure out how to catch signals from the server and not fall into a trap.
The server is like a good old professor. He won't write long letters every time. It uses HTTP codes, or three-digit signals, in its responses to help you (and other developers) understand what's going on. They are divided into five categories, like respected age groups, from the wise informational (1xx) to the capricious erroneous (4xx and 5xx).
Here are our main characters:
How can you tell if the server is happy? Codes 2xx
The server, like any introvert, is simply happy if you did everything right. Here are his signals of approval:
HttpURLConnection connection = (HttpURLConnection) new URL("https://easy.java.com/data").openConnection(); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { System.out.println("Сервер сказал ОК! Тянем данные..."); }
There may already be problems here, like if you accidentally **opened the wrong door**.
int responseCode = connection.getResponseCode(); if (responseCode == 404) { System.out.println("Сервер говорит, что ничего не нашел."); } else if (responseCode == 401) { System.out.println("Ой, кажется, сюда нужен пароль."); }
And then the server can’t stand it. He wanted to work, but somewhere something clearly went wrong.
When you work with Spring, you have a RestTemplate and WebClient for requests. They allow you to catch server responses like a real fisherman.
HttpURLConnection connection = (HttpURLConnection) new URL("https://easy.java.com/data").openConnection(); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { System.out.println("Сервер сказал ОК! Тянем данные..."); }
WebClient is a tool for those who are not afraid to work in the “waiting, but not right away” style. It’s as if you’re saying: “Server, do what you can, and I’ll wait for now.”
int responseCode = connection.getResponseCode(); if (responseCode == 404) { System.out.println("Сервер говорит, что ничего не нашел."); } else if (responseCode == 401) { System.out.println("Ой, кажется, сюда нужен пароль."); }
Check the timeouts - sometimes the server freezes, and you should be on time for lunch. Set the waiting time.
Don't forget about logging - every code is important! Log the answers and you will always be able to understand what went wrong.
Work with 4xx and 5xx - learn from mistakes. The better you handle such responses, the more robust your application will be.
The server is a capricious creature, and every time you receive a code from it, it’s like you’re solving a rebus. But, knowing the basic codes and their meaning, you can quickly understand what is required of you!
Remember: HTTP codes are the language of communication between your Java code and the server. Know how to read it, and the server will always be happy.
The above is the detailed content of Guide to HTTP Codes in Java: How to understand what the server wants from you?. For more information, please follow other related articles on the PHP Chinese website!