我想从图像中获取验证码,但是我无法使用Httpclient getMethord下载图像。 HttpClient收到状态码202,浏览器可以获取该图像。
我使用Httpclient版本4.5.8 Jar。
String url = "https://wbca.cde.org.cn/wbca/jcaptcha";
HttpClient httpClient = new DefaultHttpClient();
HttpGet getMethod = new HttpGet(url);
try {
HttpResponse response;
response = httpClient.execute(getMethod, new BasicHttpContext());
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
OutputStream outstream = new FileOutputStream(new File("D:\\123.jpg"));
int l = -1;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
outstream.write(tmp);
}
outstream.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
getMethod.releaseConnection();
}
答案 0 :(得分:0)
从设置的URL“ https://wbca.cde.org.cn/wbca/jcaptcha”返回的内容不是图像文件,而是HTML页面(使用JavaScript在浏览器上形成图像)。请参阅响应标题:
< HTTP/1.1 202 Accepted
< Date: Fri, 19 Apr 2019 08:32:43 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Server: ******
< Pragma: no-cache
< Cache-Control: no-cache
< Expires: Fri, 19 Apr 2019 08:28:55 GMT