@Override
  public InputStream execute(CloseableHttpClient httpclient, String uri, String queryParam)
      throws WxErrorException, IOException {
    if (queryParam != null) {
      if (uri.indexOf('?') == -1) {
        uri += '?';
      }
      uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
    }

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {

      Header[] contentTypeHeader = response.getHeaders("Content-Type");
      if (contentTypeHeader != null && contentTypeHeader.length > 0) {
        // 下载媒体文件出错
        if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
          String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
          throw new WxErrorException(JSON.parseObject(responseContent, WxError.class));
        }
      }
      InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);

      // 视频文件不支持下载
      String fileName = getFileName(response);
      if (StringUtils.isBlank(fileName)) {
        return null;
      }

      return inputStream;
    }
  }
Esempio n. 2
0
 private ContentType receiveWithCharsetParameter(ContentType contentType, Charset charset) {
   if (contentType.getCharset() != null) {
     return contentType;
   }
   final String mimeType = contentType.getMimeType();
   if (mimeType.equals(ContentType.TEXT_PLAIN.getMimeType())
       || AbstractFutureCallback.ODATA_MIME_TYPE.matcher(mimeType).matches()) {
     return contentType.withCharset(charset);
   }
   return contentType;
 }