forked from chanjarster/weixin-java-tools
-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
http框架的选用说明
Binary Wang edited this page Dec 22, 2017
·
9 revisions
目前cp和mp均支持多种http框架(apache-httpclient、jodd-http以及okhttp)的自由选用,pay只支持apache-httpclient和jodd-http, 客户端默认情况下可以使用apache httpclient;
拿MP举例子来说: (特别说明,以防新手不假思索造成对现有项目的影响。如果在你的项目中其他地方还会使用到httpclient,那请注意不要填加exclusions的部分)
1、 如果想使用jodd-http,请在项目pom文件中如下配置:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${weixin-java-mp.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jodd</groupId>
<artifactId>jodd-http</artifactId>
<version>3.7.1</version>
</dependency>
此时应该使用的 WxMpService
实现类应该是:
me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl
也可以参考https://github.com/wechat-group/weixin-java-mp-demo 的jodd-http分支,来查看相关代码。
2、如果想使用okhttp,请在项目pom文件中如下配置:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>${weixin-java-mp.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.7.0</version>
</dependency>
此时应该使用的 WxMpService
实现类应该是:
me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl
也可以参考https://github.com/wechat-group/weixin-java-mp-demo 的okhttp分支,来查看相关代码。
String encType = request.getParameter("encrypt_type");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String msgSignature = request.getParameter("msg_signature");
String signature = request.getParameter("signature");
String openid = request.getParameter("openid");
log.info("接收微信请求:[openid=[{}], [openid=[{}],[signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[{}] ",
appid, openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
// 请求里面没有对应信息。 // if (!this.wxMpService.switchover(appid)) { //appid // throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid)); // }
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}
return ResponseEntity.ok("");