Skip to content

Commit

Permalink
Fix issue binarywang#2301: 修复OKhttp与Jodd的生成小程序码实现类当微信后端报错时不会抛异常问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyi committed Sep 6, 2021
1 parent 5f35ab2 commit dda69d9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType
HttpResponse response = request.send();
response.charset(StandardCharsets.UTF_8.name());
String contentTypeHeader = response.header("Content-Type");
if (MimeTypes.MIME_TEXT_PLAIN.equals(contentTypeHeader)) {
if (MimeTypes.MIME_APPLICATION_JSON.equals(contentTypeHeader)) {
String responseContent = response.bodyText();
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}
try (InputStream inputStream = new ByteArrayInputStream(response.bodyBytes())) {
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
if (StringUtils.isBlank(filePath)) {
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public OkHttpQrcodeBytesRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInf
*/
@Override
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("text/plain; charset=utf-8"));
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 ("text/plain".equals(contentTypeHeader)) {
if (null != contentTypeHeader && contentTypeHeader.startsWith("application/json")) {
String responseContent = response.body().string();
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}

try (InputStream inputStream = response.body().byteStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public OkHttpQrcodeFileRequestExecutor(RequestHttp<OkHttpClient, OkHttpProxyInfo
*/
@Override
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
RequestBody body = RequestBody.Companion.create(qrcodeWrapper.toJson(), MediaType.parse("text/plain; charset=utf-8"));
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 ("text/plain".equals(contentTypeHeader)) {
if (null != contentTypeHeader && contentTypeHeader.startsWith("application/json")) {
String responseContent = response.body().string();
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}

try (InputStream inputStream = response.body().byteStream()) {
Expand Down

0 comments on commit dda69d9

Please sign in to comment.