From fa4cc5a9fd72d365bca1adfde74acd450b1cc536 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Wed, 22 Nov 2017 23:48:59 +0800 Subject: [PATCH] =?UTF-8?q?#380=20=E4=BF=AE=E5=A4=8Dokhttp=E5=92=8Cjodd-ht?= =?UTF-8?q?tp=E5=AE=9E=E7=8E=B0=E7=9A=84=E8=8E=B7=E5=8F=96=E6=B0=B8?= =?UTF-8?q?=E4=B9=85=E7=B4=A0=E6=9D=90=E6=8E=A5=E5=8F=A3=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apache/ApacheMaterialNewsInfoRequestExecutor.java | 5 ++--- .../http/jodd/JoddMaterialNewsInfoRequestExecutor.java | 8 +++++--- .../okhttp/OkhttpMaterialNewsInfoRequestExecutor.java | 5 ++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java index fe8dc580..baab51b5 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/apache/ApacheMaterialNewsInfoRequestExecutor.java @@ -1,5 +1,6 @@ package me.chanjar.weixin.mp.util.http.apache; +import com.google.common.collect.ImmutableMap; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.RequestHttp; @@ -43,9 +44,7 @@ public class ApacheMaterialNewsInfoRequestExecutor httpPost.setConfig(config); } - Map params = new HashMap<>(); - params.put("media_id", materialId); - httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params))); + httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)))); try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); this.logger.debug("响应原始数据:{}", responseContent); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java index 181a6407..2bce6ee4 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/jodd/JoddMaterialNewsInfoRequestExecutor.java @@ -1,5 +1,6 @@ package me.chanjar.weixin.mp.util.http.jodd; +import com.google.common.collect.ImmutableMap; import jodd.http.HttpConnectionProvider; import jodd.http.HttpRequest; import jodd.http.HttpResponse; @@ -9,6 +10,7 @@ import jodd.util.StringPool; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.RequestHttp; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; @@ -28,13 +30,13 @@ public class JoddMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequest @Override public WxMpMaterialNews 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); + HttpRequest request = HttpRequest.post(uri) + .withConnectionProvider(requestHttp.getRequestHttpClient()) + .body(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))); HttpResponse response = request.send(); response.charset(StringPool.UTF_8); diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java index 3f46ab78..e8bd9bee 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/http/okhttp/OkhttpMaterialNewsInfoRequestExecutor.java @@ -1,9 +1,11 @@ package me.chanjar.weixin.mp.util.http.okhttp; +import com.google.common.collect.ImmutableMap; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.RequestHttp; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews; import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; @@ -28,7 +30,8 @@ public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoReque //得到httpClient OkHttpClient client = requestHttp.getRequestHttpClient(); - RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build(); + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), + WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))); Request request = new Request.Builder().url(uri).post(requestBody).build(); Response response = client.newCall(request).execute();