为什么Httpclient只获取202,但浏览器却可以获取图像内容

时间:2019-04-19 08:24:33

标签: java httpclient

我想从图像中获取验证码,但是我无法使用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();
  }

1 个答案:

答案 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