| public class WeixinAPIHelper { |
005 | private String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; |
009 | private String getUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}"; |
013 | private String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token={0}"; |
015 | private HttpClient webClient; |
017 | private Log log = LogFactory.getLog(getClass()); |
018 | public void initWebClient(String proxyHost, int proxyPort){ |
019 | this.initWebClient(); |
020 | if(webClient != null && !StringUtils.isEmpty(proxyHost)){ |
021 | HttpHost proxy = new HttpHost(proxyHost, proxyPort); |
022 | webClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); |
027 | * @desc 初始化創(chuàng)建 WebClient |
029 | public void initWebClient() { |
030 | log.info("initWebClient start...."); |
032 | PoolingClientConnectionManager tcm = new PoolingClientConnectionManager(); |
034 | SSLContext ctx = SSLContext.getInstance("TLS"); |
035 | X509TrustManager tm = new X509TrustManager() { |
037 | public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
041 | public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
045 | public X509Certificate[] getAcceptedIssuers() { |
049 | ctx.init(null, new X509TrustManager[] { tm }, null); |
050 | SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
051 | Scheme sch = new Scheme("https", 443, ssf); |
052 | tcm.getSchemeRegistry().register(sch); |
053 | webClient = new DefaultHttpClient(tcm); |
054 | } catch (Exception ex) { |
055 | log.error("initWebClient exception", ex); |
057 | log.info("initWebClient end...."); |
062 | * @desc 獲取授權(quán)token |
067 | public String getAccessToken(String appid, String secret) { |
068 | String accessToken = null; |
070 | log.info("getAccessToken start.{appid=" + appid + ",secret:" + secret + "}"); |
071 | String url = MessageFormat.format(this.getTokenUrl, appid, secret); |
072 | String response = executeHttpGet(url); |
073 | accessToken = JsonUtils.read(response, "access_token"); |
074 | } catch (Exception e) { |
075 | log.error("get access toekn exception", e); |
085 | public String sendMessage(String token,String msg){ |
087 | log.info("sendMessage start.token:"+token+",msg:"+msg); |
088 | String url = MessageFormat.format(this.sendMsgUrl, token); |
089 | HttpPost post = new HttpPost(url); |
090 | ResponseHandler<?> responseHandler = new BasicResponseHandler(); |
091 | StringEntity entity = new StringEntity(msg); |
092 | post.setEntity(entity); |
093 | String response = (String) this.webClient.execute(post, responseHandler); |
094 | log.info("return response=====start======"); |
096 | log.info("return response=====end======"); |
099 | }catch (Exception e) { |
100 | log.error("get user info exception", e); |
110 | public WeixinOpenUser getUserInfo(String token, String openid) { |
112 | log.info("getUserInfo start.{token:" + token + ",openid:" + openid + "}"); |
113 | String url = MessageFormat.format(this.getUserInfoUrl, token, openid); |
114 | String response = executeHttpGet(url); |
115 | JsonNode json = JsonUtils.read(response); |
116 | if (json.get("openid") != null) { |
117 | WeixinOpenUser user = new WeixinOpenUser(); |
118 | user.setOpenUserId(json.get("openid").asText()); |
119 | user.setState(json.get("subscribe").asText()); |
120 | if ("1".equals(user.getState())) { |
121 | user.setUserName(json.get("nickname").asText()); |
122 | user.setSex(json.get("sex").asText()); |
123 | user.setCity(json.get("city").asText()); |
124 | user.setLanguage(json.get("language").asText()); |
128 | } catch (Exception e) { |
129 | log.error("get user info exception", e); |
135 | * @desc 發(fā)起HTTP GET請(qǐng)求返回?cái)?shù)據(jù) |
138 | * @throws IOException |
139 | * @throws ClientProtocolException |
141 | private String executeHttpGet(String url) throws IOException, ClientProtocolException { |
142 | ResponseHandler<?> responseHandler = new BasicResponseHandler(); |
143 | String response = (String) this.webClient.execute(new HttpGet(url), responseHandler); |
144 | log.info("return response=====start======"); |
146 | log.info("return response=====end======"); |
|