Java 是否有任何内置类包含HTTP响应代码及其类别?
当我需要检查一个响应代码是否是成功代码之一(2xx)时,我通常会创建一个整数数组并查看它。比如说,Java 是否有任何内置类包含HTTP响应代码及其类别?,java,http-response-codes,Java,Http Response Codes,当我需要检查一个响应代码是否是成功代码之一(2xx)时,我通常会创建一个整数数组并查看它。比如说, public static final int[] SUCCESS_STATUS_CODES = new int[]{ 200, 201, 202, 203, 204, 205, 206, 207, 208, 226 }; //pseudo code if(responseCode is from success 2xx category) do something els
public static final int[] SUCCESS_STATUS_CODES = new int[]{
200, 201, 202, 203, 204, 205, 206, 207, 208, 226
};
//pseudo code
if(responseCode is from success 2xx category)
do something
else if (responseCode is from error 4xx category
do something
我对其他响应代码也这样做
我想知道是否有一个内置对象已经保存了所有这些代码并定期更新它们,我可以使用它们
比如说,
public static final int[] SUCCESS_STATUS_CODES = new int[]{
200, 201, 202, 203, 204, 205, 206, 207, 208, 226
};
//pseudo code
if(responseCode is from success 2xx category)
do something
else if (responseCode is from error 4xx category
do something
据我所知,标准库中没有这样的类。我知道最接近的if是
java.net.HttpURLConnection
,它为各种代码提供静态常量
但你似乎要做的工作比你需要做的要多。HTTP响应代码类别内置于其值中。你应该能够做到以下几点:
switch (responseCode / 100) {
case 1:
/* informational response */
break;
case 2:
/* success response */
break;
case 3:
/* redirection response */
break;
case 4:
/* client error response */
break;
case 5:
/* server error response */
break;
default:
/* bad response code */
break;
}
据我所知,标准库中没有这样的类。我知道最接近的if是
java.net.HttpURLConnection
,它为各种代码提供静态常量
但你似乎要做的工作比你需要做的要多。HTTP响应代码类别内置于其值中。你应该能够做到以下几点:
switch (responseCode / 100) {
case 1:
/* informational response */
break;
case 2:
/* success response */
break;
case 3:
/* redirection response */
break;
case 4:
/* client error response */
break;
case 5:
/* server error response */
break;
default:
/* bad response code */
break;
}
据我所知,标准库中没有这样的类。我知道最接近的if是
java.net.HttpURLConnection
,它为各种代码提供静态常量
但你似乎要做的工作比你需要做的要多。HTTP响应代码类别内置于其值中。你应该能够做到以下几点:
switch (responseCode / 100) {
case 1:
/* informational response */
break;
case 2:
/* success response */
break;
case 3:
/* redirection response */
break;
case 4:
/* client error response */
break;
case 5:
/* server error response */
break;
default:
/* bad response code */
break;
}
据我所知,标准库中没有这样的类。我知道最接近的if是
java.net.HttpURLConnection
,它为各种代码提供静态常量
但你似乎要做的工作比你需要做的要多。HTTP响应代码类别内置于其值中。你应该能够做到以下几点:
switch (responseCode / 100) {
case 1:
/* informational response */
break;
case 2:
/* success response */
break;
case 3:
/* redirection response */
break;
case 4:
/* client error response */
break;
case 5:
/* server error response */
break;
default:
/* bad response code */
break;
}
org.springframework.http.HttpStatus
有一个方法HttpStatus.is2xxsuccesful()
返回一个布尔值。它是Spring的一部分,没有内置到Java中,但希望对某些人仍然有用。org.springframework.http.HttpStatus
有一个方法HttpStatus.is2xxsuccesful()
返回布尔值。它是Spring的一部分,没有内置到Java中,但希望对某些人仍然有用。org.springframework.http.HttpStatus
有一个方法HttpStatus.is2xxsuccesful()
返回布尔值。它是Spring的一部分,没有内置到Java中,但希望对某些人仍然有用。org.springframework.http.HttpStatus
有一个方法HttpStatus.is2xxsuccesful()
返回布尔值。它是Spring的一部分,没有内置到Java中,但希望对某些人仍然有用。您使用的HTTP客户端是什么?重复的I useHttpURLConnection
。Android开发。@RichardNeish你的链接基本上回答了我的问题,但没有提到类别。检查我的伪代码。你使用什么HTTP客户端?重复我使用的HttpURLConnection
。Android开发。@RichardNeish你的链接基本上回答了我的问题,但没有提到类别。检查我的伪代码。你使用什么HTTP客户端?重复我使用的HttpURLConnection
。Android开发。@RichardNeish你的链接基本上回答了我的问题,但没有提到类别。检查我的伪代码。你使用什么HTTP客户端?重复我使用的HttpURLConnection
。Android开发。@RichardNeish你的链接基本上回答了我的问题,但没有提到类别。检查我的伪代码。