int StatusCode = httpResponse.getStatusLine().getStatusCode();
处理返回结果:
/**
* 处理返回结果
* @param response
* @return
*/
private static HttpClientResult handleHttpClientResult(CloseableHttpResponse response){
HttpEntity responseEntity = null;
HttpClientResult httpClientResult;
if(response != null && response.getStatusLine() != null){
int code = response.getStatusLine().getStatusCode();
String content = "";
responseEntity = response.getEntity();
if(responseEntity != null){
try {
content = EntityUtils.toString(response.getEntity(), ENCODING);
} catch (IOException e) {
e.printStackTrace();
}
}
httpClientResult = new HttpClientResult(code,content);
}else{
// 访问一个连接被拒绝的网站,eg:www.baidubucunzai.com
httpClientResult = new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
return httpClientResult;
}
HttpClientResult:
package pojo; /**
* @description:
* @author: lv
* @time: 2020/6/1 13:53
*/
public class HttpClientResult { /**
* 响应状态码
*/
private int code; /**
* 响应数据
*/
private String content; public HttpClientResult(int code){
this.code = code;
} public HttpClientResult(int code,String content){
this.code = code;
this.content = content;
} public int getCode() {
return code;
} public void setCode(int code) {
this.code = code;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
}
}
int StatusCode = httpResponse.getStatusLine().getStatusCode();