All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.binarywang.wx.miniapp.executor.OkHttpQrcodeBytesRequestExecutor Maven / Gradle / Ivy

There is a newer version: 4.6.7.B
Show newest version
package cn.binarywang.wx.miniapp.executor;

import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import okhttp3.*;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author wenqiang
 * @since 2020/12/25
 */
public class OkHttpQrcodeBytesRequestExecutor extends QrcodeBytesRequestExecutor {


  public OkHttpQrcodeBytesRequestExecutor(RequestHttp requestHttp) {
    super(requestHttp);
  }

  /**
   * 执行http请求.
   *
   * @param uri           uri
   * @param qrcodeWrapper 数据
   * @param wxType        微信模块类型
   * @return 响应结果
   * @throws WxErrorException 自定义异常
   * @throws IOException      io异常
   */
  @Override
  public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
    RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("application/json; charset=utf-8"));
    Request request = new Request.Builder().url(uri).post(body).build();
    Response response = requestHttp.getRequestHttpClient().newCall(request).execute();
    String contentTypeHeader = response.header("Content-Type");
    if (null != contentTypeHeader && contentTypeHeader.startsWith("application/json")) {
      String responseContent = response.body().string();
      throw new WxErrorException(WxError.fromJson(responseContent, wxType));
    }

    try (InputStream inputStream = response.body().byteStream()) {
      return IOUtils.toByteArray(inputStream);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy