Java HttpURLConnection getResponseCode

The getResponseCode is a method of Java HttpURLConnection class. This method is used to get the status code from the HTTP response message. For example, if the status line is HTTP/1.0 200 OK

Then it will return 200, or if the status line is HTTP/1.0 401 unauthorized, then it will return 401. These method returns -1 if the response from HTTP is not valid.

Syntax

Returns

It returns the status code of HTTP or -1

Throws

IOException - It throws this exception if an error occurs while connecting the server.

Example 1

Test it Now

Output:

https://www.mysql.com
Connection Response Code : 200
Connection Disconnect...

Example 2

Test it Now

Output:

https://www.Facebook.com
Connection Response Code : 203 200
https://www.javatpoint.com
Connection Response Code : 303 200
Connection Disconnect...




Latest Courses