Java Code Examples for jodd.http.HttpRequest#withConnectionProvider()

The following examples show how to use jodd.http.HttpRequest#withConnectionProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MaterialDeleteJoddHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  } else {
    return true;
  }
}
 
Example 2
Source File: MaterialVideoInfoJoddHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMpMaterialVideoInfoResult execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  } else {
    return WxMpMaterialVideoInfoResult.fromJson(responseContent);
  }
}
 
Example 3
Source File: MediaImgUploadHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMediaImgUploadResult execute(String uri, File data) throws WxErrorException, IOException {
  if (data == null) {
    throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("文件对象为空").build());
  }

  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.form("media", data);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent, WxType.MP);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  }

  return WxMediaImgUploadResult.fromJson(responseContent);
}
 
Example 4
Source File: JoddHttpMediaUploadRequestExecutor.java    From weixin-java-tools with Apache License 2.0 6 votes vote down vote up
@Override
public WxMediaUploadResult execute(String uri, File file) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());
  request.form("media", file);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  WxError error = WxError.fromJson(responseContent);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  }
  return WxMediaUploadResult.fromJson(responseContent);
}
 
Example 5
Source File: QrCodeJoddHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public File execute(String uri, WxMpQrCodeTicket ticket) throws WxErrorException, IOException {
  if (ticket != null) {
    if (uri.indexOf('?') == -1) {
      uri += '?';
    }
    uri += uri.endsWith("?")
      ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8")
      : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
  }

  HttpRequest request = HttpRequest.get(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  String contentTypeHeader = response.header("Content-Type");
  if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
    String responseContent = response.bodyText();
    throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
  }
  try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
    return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
  }
}
 
Example 6
Source File: MaterialVoiceAndImageDownloadJoddHttpRequestExecutor.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream execute(String uri, String materialId) throws WxErrorException, IOException {
  HttpRequest request = HttpRequest.post(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  request.query("media_id", materialId);
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);
  try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
    // 下载媒体文件出错
    byte[] responseContent = IOUtils.toByteArray(inputStream);
    String responseContentString = new String(responseContent, StandardCharsets.UTF_8);
    if (responseContentString.length() < 100) {
      try {
        WxError wxError = WxGsonBuilder.create().fromJson(responseContentString, WxError.class);
        if (wxError.getErrorCode() != 0) {
          throw new WxErrorException(wxError);
        }
      } catch (com.google.gson.JsonSyntaxException ex) {
        return new ByteArrayInputStream(responseContent);
      }
    }
    return new ByteArrayInputStream(responseContent);
  }
}
 
Example 7
Source File: JoddHttpSimplePostRequestExecutor.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String execute(String uri, String postEntity) throws WxErrorException, IOException {
  HttpConnectionProvider provider = requestHttp.getRequestHttpClient();
  ProxyInfo proxyInfo = requestHttp.getRequestHttpProxy();

  HttpRequest request = HttpRequest.post(uri);
  if (proxyInfo != null) {
    provider.useProxy(proxyInfo);
  }
  request.withConnectionProvider(provider);
  if (postEntity != null) {
    request.bodyText(postEntity);
  }
  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String responseContent = response.bodyText();
  if (responseContent.isEmpty()) {
    throw new WxErrorException(WxError.builder().errorCode(9999).errorMsg("无响应内容")
      .build());
  }

  if (responseContent.startsWith("<xml>")) {
    //xml格式输出直接返回
    return responseContent;
  }

  WxError error = WxError.fromJson(responseContent);
  if (error.getErrorCode() != 0) {
    throw new WxErrorException(error);
  }
  return responseContent;
}
 
Example 8
Source File: JoddHttpMediaDownloadRequestExecutor.java    From weixin-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public File execute(String uri, String queryParam) throws WxErrorException, IOException {
  if (queryParam != null) {
    if (uri.indexOf('?') == -1) {
      uri += '?';
    }
    uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
  }

  HttpRequest request = HttpRequest.get(uri);
  if (requestHttp.getRequestHttpProxy() != null) {
    requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
  }
  request.withConnectionProvider(requestHttp.getRequestHttpClient());

  HttpResponse response = request.send();
  response.charset(StringPool.UTF_8);

  String contentType = response.header("Content-Type");
  if (contentType != null && contentType.startsWith("application/json")) {
    // application/json; encoding=utf-8 下载媒体文件出错
    throw new WxErrorException(WxError.fromJson(response.bodyText()));
  }

  String fileName = new HttpResponseProxy(response).getFileName();
  if (StringUtils.isBlank(fileName)) {
    return null;
  }

  try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
    return FileUtils.createTmpFile(inputStream,
      FilenameUtils.getBaseName(fileName),
      FilenameUtils.getExtension(fileName),
      super.tmpDirFile);
  }
}