求助!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; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
先判断一下是不是服务器本身就响应失败了,如果不是,把http的信息全部打印出来,然后在浏览器里用http trace工具观察一下正常流程,对比两者有什么不同,看看是不是client 请求发的有问题
传入的url错了吧,正确的url要带http://或https,你是不是忘了
引用来自“BoXuan”的评论
不要判断content-type,http协议应该会自动以字节流传送图片
不要判断content-type,http协议应该会自动以字节流传送图片