How can I convert HTTP status code to its text representation, in Java? I mean are there any existing implementations of such a conversion. The best I've found so far is java.ws.rs.core.Response.Status#fromStatusCode()
, which converts only a limited subset of all statuses.
To get the response body as a string we can use the EntityUtils. toString() method. This method read the content of an HttpEntity object content and return it as a string. The content will be converted using the character set from the entity object.
HTTP status codes are part of the status-line of a HTTP response. These 3-digit integer codes indicate the result of the servers attempt to satisfy the request. The first digit of the status-code is used to categorize the response: 1xx: Informal. 2xx: Success, the request has been understood and accepted.
In the main window of the program, enter your website homepage URL and click on the 'Start' button. As soon as crawling is complete, you will have all status codes in the corresponding table column. Pages with the 4xx and 5xx HTTP status codes will be gathered into special issue reports.
The HTTP Status 200 (OK) status code indicates that the request has been processed successfully on the server. The response payload depends on the HTTP method which was selected for the request.
If you are working with Spring, or are okay with importing Spring Web, then this would do it:
First import it:
import org.springframework.http.HttpStatus;
Then use it like:
int statusCode = 502;
String message = HttpStatus.valueOf(statusCode).getReasonPhrase();
Apache HttpComponents has an (old-style) enum class which does this:
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpStatus.html
You can call itsgetStatusText
method with an enum instance as the argument to get the text representation of a status code.
Maven dependency is:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
If you're happy to import Spring web, org.springframework.http.HttpStatus.valueOf(int).name()
should do, if you don't mind underscores.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With