• (六)获取http状态码和处理返回结果


    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();

  • 相关阅读:
    Zookeeper搭建集群及协同
    无限可能,Elasticsearch(一)
    如何打造一款m3u8视频爬虫
    垃圾如何进行自救
    必知必会的JavaJDK工具
    如何在局域网内开一家电影院
    谈谈网络游戏中的延迟解决方案
    简述一致性哈希算法
    Python网络爬虫实战(五)批量下载B站收藏夹视频
    Python网络爬虫实战(四)模拟登录
  • 原文地址:https://www.cnblogs.com/lvchengda/p/13036070.html
Copyright © 2020-2023  润新知