微信微信java开发之实现微信主动推送消息开发之实现微信主动推送消息
主要介绍了微信开发过程中的使用java实现微信主动推送消息示例,需要的朋友可以参考下
1.拉取access_token
2.拉取用户信息
3.主动推送消息
4.接口貌似要申请权限
5.依赖httpclient4.2.3 和jackson 2.2.1
复制代码 代码如下:
public class WeixinAPIHelper {
/**
* 获取token接口
*/
private String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
/**
* 拉微信用户信息接口
*/
private String getUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}";
/**
* 主动推送信息接口
*/
private String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token={0}";
private HttpClient webClient;
private Log log = LogFactory.getLog(getClass());
public void initWebClient(String proxyHost, int proxyPort){
this.initWebClient();
if(webClient != null && !StringUtils.isEmpty(proxyHost)){
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
webClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
}
/**
* @desc 初始化创建 WebClient
*/
public void initWebClient() {
log.info("initWebClient start....");
try {
PoolingClientConnectionManager tcm = new PoolingClientConnectionManager();
tcm.setMaxTotal(10);
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new X509TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme sch = new Scheme("https", 443, ssf);
tcm.getSchemeRegistry().register(sch);
webClient = new DefaultHttpClient(tcm);
} catch (Exception ex) {
log.error("initWebClient exception", ex);
} finally {
评论0