21
21
import org .apache .http .client .config .RequestConfig ;
22
22
import org .apache .http .client .methods .CloseableHttpResponse ;
23
23
import org .apache .http .client .methods .HttpGet ;
24
+ import org .apache .http .conn .ssl .DefaultHostnameVerifier ;
24
25
import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
25
26
import org .apache .http .impl .client .BasicResponseHandler ;
26
27
import org .apache .http .impl .client .CloseableHttpClient ;
@@ -209,6 +210,13 @@ public WxMpMassSendResult massOpenIdsMessageSend(WxMpMassOpenIdsMessage message)
209
210
return WxMpMassSendResult .fromJson (responseContent );
210
211
}
211
212
213
+ @ Override
214
+ public WxMpMassSendResult massMessagePreview (WxMpMassPreviewMessage wxMpMassPreviewMessage ) throws Exception {
215
+ String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview" ;
216
+ String responseContent = execute (new SimplePostRequestExecutor (), url , wxMpMassPreviewMessage .toJson ());
217
+ return WxMpMassSendResult .fromJson (responseContent );
218
+ }
219
+
212
220
@ Override
213
221
public String shortUrl (String long_url ) throws WxErrorException {
214
222
String url = "https://api.weixin.qq.com/cgi-bin/shorturl" ;
@@ -226,11 +234,30 @@ public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorEx
226
234
String responseContent = execute (new SimplePostRequestExecutor (), url , templateMessage .toJson ());
227
235
JsonElement tmpJsonElement = Streams .parse (new JsonReader (new StringReader (responseContent )));
228
236
final JsonObject jsonObject = tmpJsonElement .getAsJsonObject ();
229
- if (jsonObject .get ("errcode" ).getAsInt () == 0 )
237
+ if (jsonObject .get ("errcode" ).getAsInt () == 0 ){
230
238
return jsonObject .get ("msgid" ).getAsString ();
239
+ }
240
+
231
241
throw new WxErrorException (WxError .fromJson (responseContent ));
232
242
}
233
243
244
+ @ Override
245
+ public String setIndustry (WxMpIndustry wxMpIndustry ) throws WxErrorException {
246
+ if (null == wxMpIndustry .getPrimaryIndustry () || null == wxMpIndustry .getPrimaryIndustry ().getId ()
247
+ || null == wxMpIndustry .getSecondIndustry () || null == wxMpIndustry .getSecondIndustry ().getId ()) {
248
+ throw new IllegalArgumentException ("industry id is empty" );
249
+ }
250
+ String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry" ;
251
+ return execute (new SimplePostRequestExecutor (), url , wxMpIndustry .toJson ());
252
+ }
253
+
254
+ @ Override
255
+ public WxMpIndustry getIndustry () throws WxErrorException {
256
+ String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry" ;
257
+ String responseContent = execute (new SimpleGetRequestExecutor (), url , null );
258
+ return WxMpIndustry .fromJson (responseContent );
259
+ }
260
+
234
261
@ Override
235
262
public WxMpSemanticQueryResult semanticQuery (WxMpSemanticQuery semanticQuery ) throws WxErrorException {
236
263
String url = "https://api.weixin.qq.com/semantic/semproxy/search" ;
@@ -253,6 +280,16 @@ public String oauth2buildAuthorizationUrl(String redirectURI, String scope, Stri
253
280
return url .toString ();
254
281
}
255
282
283
+ private WxMpOAuth2AccessToken getOAuth2AccessToken (StringBuilder url ) throws WxErrorException {
284
+ try {
285
+ RequestExecutor <String , String > executor = new SimpleGetRequestExecutor ();
286
+ String responseText = executor .execute (this .getHttpclient (), this .httpProxy , url .toString (), null );
287
+ return WxMpOAuth2AccessToken .fromJson (responseText );
288
+ } catch (IOException e ) {
289
+ throw new RuntimeException (e );
290
+ }
291
+ }
292
+
256
293
@ Override
257
294
public WxMpOAuth2AccessToken oauth2getAccessToken (String code ) throws WxErrorException {
258
295
StringBuilder url = new StringBuilder ();
@@ -262,13 +299,7 @@ public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorExc
262
299
url .append ("&code=" ).append (code );
263
300
url .append ("&grant_type=authorization_code" );
264
301
265
- try {
266
- RequestExecutor <String , String > executor = new SimpleGetRequestExecutor ();
267
- String responseText = executor .execute (getHttpclient (), this .httpProxy , url .toString (), null );
268
- return WxMpOAuth2AccessToken .fromJson (responseText );
269
- } catch (IOException e ) {
270
- throw new RuntimeException (e );
271
- }
302
+ return this .getOAuth2AccessToken (url );
272
303
}
273
304
274
305
@ Override
@@ -279,13 +310,7 @@ public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throw
279
310
url .append ("&grant_type=refresh_token" );
280
311
url .append ("&refresh_token=" ).append (refreshToken );
281
312
282
- try {
283
- RequestExecutor <String , String > executor = new SimpleGetRequestExecutor ();
284
- String responseText = executor .execute (getHttpclient (), this .httpProxy , url .toString (), null );
285
- return WxMpOAuth2AccessToken .fromJson (responseText );
286
- } catch (IOException e ) {
287
- throw new RuntimeException (e );
288
- }
313
+ return this .getOAuth2AccessToken (url );
289
314
}
290
315
291
316
@ Override
@@ -440,10 +465,7 @@ private void initHttpClient() {
440
465
441
466
if (this .wxMpConfigStorage .getSSLContext () != null ){
442
467
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (
443
- this .wxMpConfigStorage .getSSLContext (),
444
- new String [] { "TLSv1" },
445
- null ,
446
- SSLConnectionSocketFactory .BROWSER_COMPATIBLE_HOSTNAME_VERIFIER );
468
+ this .wxMpConfigStorage .getSSLContext (), new String [] { "TLSv1" }, null , new DefaultHostnameVerifier ());
447
469
apacheHttpClientBuilder .sslConnectionSocketFactory (sslsf );
448
470
}
449
471
@@ -460,36 +482,11 @@ public void setRetrySleepMillis(int retrySleepMillis) {
460
482
this .retrySleepMillis = retrySleepMillis ;
461
483
}
462
484
463
-
464
485
@ Override
465
486
public void setMaxRetryTimes (int maxRetryTimes ) {
466
487
this .maxRetryTimes = maxRetryTimes ;
467
488
}
468
489
469
- @ Override
470
- public WxMpMassSendResult massMessagePreview (WxMpMassPreviewMessage wxMpMassPreviewMessage ) throws Exception {
471
- String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview" ;
472
- String responseContent = execute (new SimplePostRequestExecutor (), url , wxMpMassPreviewMessage .toJson ());
473
- return WxMpMassSendResult .fromJson (responseContent );
474
- }
475
-
476
- @ Override
477
- public String setIndustry (WxMpIndustry wxMpIndustry ) throws WxErrorException {
478
- if (null == wxMpIndustry .getPrimaryIndustry () || null == wxMpIndustry .getPrimaryIndustry ().getId ()
479
- || null == wxMpIndustry .getSecondIndustry () || null == wxMpIndustry .getSecondIndustry ().getId ()) {
480
- throw new IllegalArgumentException ("industry id is empty" );
481
- }
482
- String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry" ;
483
- return execute (new SimplePostRequestExecutor (), url , wxMpIndustry .toJson ());
484
- }
485
-
486
- @ Override
487
- public WxMpIndustry getIndustry () throws WxErrorException {
488
- String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry" ;
489
- String responseContent = execute (new SimpleGetRequestExecutor (), url , null );
490
- return WxMpIndustry .fromJson (responseContent );
491
- }
492
-
493
490
@ Override
494
491
public WxMpKefuService getKefuService () {
495
492
return this .kefuService ;
0 commit comments