开发者社区> 问答> 正文

求助!httpclient4.4/4.5 ClientProtocolException 异常的问题

求助!httpclient4.4/4.5 在 spring环境下(test unit / tomcat)报org.apache.http.client.ClientProtocolException的异常。。写个main方法单独测试httpclient是正常的。。谁遇到过这个问题?感激

public static boolean getImage(String url, File file) throws NoSuchAlgorithmException, KeyManagementException, IOException {
        CloseableHttpClient httpclient = HttpUtils.createHttpClient(url);
 
        HttpGet httpGet = createHttpGet(url, null);
 
        CloseableHttpResponse response = httpclient.execute(httpGet);
        String contentType = response.getFirstHeader("Content-Type").getValue();
        if (contentType != null && (contentType.toLowerCase().contains("image/") ||
                contentType.toLowerCase().contains("jpg") ||
                contentType.toLowerCase().contains("png") ||
                contentType.toLowerCase().contains("gif"))) {
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
            FileUtils.writeByteArrayToFile(file, bytes);
            return true;
        } else {
            return false;
        }
    }

展开
收起
a123456678 2016-03-19 09:30:37 4886 0
1 条回答
写回答
取消 提交回答
  • 先判断一下是不是服务器本身就响应失败了,如果不是,把http的信息全部打印出来,然后在浏览器里用http trace工具观察一下正常流程,对比两者有什么不同,看看是不是client 请求发的有问题

    2019-07-17 19:07:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载