一、HTTP請求異常的種類
在處理HTTP請求異常之前,我們需要清楚的了解HTTP請求異常的種類。HTTP請求異常包括但不限於:
1、網絡異常:如DNS解析失敗、網絡連接超時等
try{
httpclient.execute(httpget)
}catch(UnknownHostException | ConnectTimeoutException e){
//處理網絡異常
}
2、服務器異常:如響應碼不是200、服務器返回的JSON信息格式不規範等
HttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
//處理服務器異常
}
3、業務異常:如用戶登錄信息錯誤、訂單不存在等
HttpResponse response = httpclient.execute(httpget);
String result = EntityUtils.toString(response.getEntity());
ResultDTO resultDTO = JSON.parseObject(result, ResultDTO.class);
if(resultDTO.getCode() != ResultCode.SUCCESS.getCode()){
//處理業務異常
}
二、如何有效處理HTTP請求異常
在開發過程中,我們需要遵循以下幾個原則來處理HTTP請求異常:
1、明確異常的類型,合理區分異常的處理方式。
2、充分利用HTTP中請求響應的狀態碼和異常信息,特別是在服務器異常時需要充分利用返回的錯誤信息來解決問題。
3、通過記錄日誌的方式,快速定位問題所在。
4、業務異常需要使用自定義異常來處理,一方面能夠更好的維護異常邏輯,另一方面能夠在異常處理層面區分業務異常與其它異常。
三、HTTP請求異常處理的最佳實踐
1、封裝工具類。將HTTP請求的重試、重定向、異常處理封裝到工具類中,使得代碼更簡潔,維護更方便。
public static String get(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
} else {
throw new RuntimeException("HTTP請求異常!");
}
} catch (IOException e) {
throw new RuntimeException("網絡異常!");
} finally {
try {
httpClient.close();
} catch (IOException e) {
throw new RuntimeException("關閉httpClient異常!");
}
}
return result;
}
2、自定義異常。通過自定義異常來對HTTP請求異常進行分類處理。
public class BusinessException extends RuntimeException {
private static final long serialVersionUID = 1L;
private Integer code;
private String message;
public BusinessException(ResultCode resultCode) {
super(resultCode.getMessage());
this.code = resultCode.getCode();
this.message = resultCode.getMessage();
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
}
3、日誌記錄。在處理異常時,需要及時記錄錯誤信息,方便進行問題定位和修復。
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
public static String get(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
HttpGet httpGet = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
} else {
logger.error("請求失敗,url: {}, statusCode: {}", url, response.getStatusLine().getStatusCode());
throw new BusinessException(ResultCode.HTTP_REQUEST_ERROR);
}
} catch (IOException e) {
logger.error("請求異常,url: {}", url, e);
throw new BusinessException(ResultCode.NETWORK_ERROR);
} finally {
try {
httpClient.close();
} catch (IOException e) {
logger.error("關閉httpClient異常", e);
throw new BusinessException(ResultCode.HTTP_CLIENT_CLOSE_ERROR);
}
}
return result;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/257480.html