| public class WeixinAPIHelper { |
002 | /** |
003 | * 獲取token接口 |
004 | */ |
005 | private String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}" ; |
006 | /** |
007 | * 拉微信用戶信息接口 |
008 | */ |
009 | private String getUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}" ; |
010 | /** |
011 | * 主動(dòng)推送信息接口 |
012 | */ |
013 | private String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token={0}" ; |
014 |
015 | private HttpClient webClient; |
016 |
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); |
023 | } |
024 | } |
025 |
026 | /** |
027 | * @desc 初始化創(chuàng)建 WebClient |
028 | */ |
029 | public void initWebClient() { |
030 | log.info( "initWebClient start...." ); |
031 | try { |
032 | PoolingClientConnectionManager tcm = new PoolingClientConnectionManager(); |
033 | tcm.setMaxTotal( 10 ); |
034 | SSLContext ctx = SSLContext.getInstance( "TLS" ); |
035 | X509TrustManager tm = new X509TrustManager() { |
036 |
037 | public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
038 |
039 | } |
040 |
041 | public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
042 |
043 | } |
044 |
045 | public X509Certificate[] getAcceptedIssuers() { |
046 | return null ; |
047 | } |
048 | }; |
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); |
056 | } finally { |
057 | log.info( "initWebClient end...." ); |
058 | } |
059 | } |
060 |
061 | /** |
062 | * @desc 獲取授權(quán)token |
063 | * @param appid |
064 | * @param secret |
065 | * @return |
066 | */ |
067 | public String getAccessToken(String appid, String secret) { |
068 | String accessToken = null ; |
069 | try { |
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); |
076 | } |
077 | return accessToken; |
078 | } |
079 | /** |
080 | * @desc 推送信息 |
081 | * @param token |
082 | * @param msg |
083 | * @return |
084 | */ |
085 | public String sendMessage(String token,String msg){ |
086 | try { |
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======" ); |
095 | log.info(response); |
096 | log.info( "return response=====end======" ); |
097 | return response; |
098 | |
099 | } catch (Exception e) { |
100 | log.error( "get user info exception" , e); |
101 | return null ; |
102 | } |
103 | } |
104 | /** |
105 | * @desc 拉取用戶信息 |
106 | * @param token |
107 | * @param openid |
108 | * @return |
109 | */ |
110 | public WeixinOpenUser getUserInfo(String token, String openid) { |
111 | try { |
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()); |
125 | } |
126 | return user; |
127 | } |
128 | } catch (Exception e) { |
129 | log.error( "get user info exception" , e); |
130 | } |
131 | return null ; |
132 | } |
133 |
134 | /** |
135 | * @desc 發(fā)起HTTP GET請(qǐng)求返回?cái)?shù)據(jù) |
136 | * @param url |
137 | * @return |
138 | * @throws IOException |
139 | * @throws ClientProtocolException |
140 | */ |
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======" ); |
145 | log.info(response); |
146 | log.info( "return response=====end======" ); |
147 | return response; |
148 | } |
149 | |
150 |
151 |
152 | } |
聯(lián)系客服