欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信java开发之实现微信主动推送消息

程序员文章站 2024-02-23 16:39:10
1.拉取access_token2.拉取用户信息3.主动推送消息4.接口貌似要申请权限5.依赖httpclient4.2.3 和jackson 2.2.1 复制代码 代码...


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 {
   log.info("initwebclient end....");
  }
 }

 /**
  * @desc 获取授权token
  * @param appid
  * @param secret
  * @return
  */
 public string getaccesstoken(string appid, string secret) {
  string accesstoken = null;
  try {
   log.info("getaccesstoken start.{appid=" + appid + ",secret:" + secret + "}");
   string url = messageformat.format(this.gettokenurl, appid, secret);
   string response = executehttpget(url);
   accesstoken = jsonutils.read(response, "access_token");
  } catch (exception e) {
   log.error("get access toekn exception", e);
  }
  return accesstoken;
 }
 /**
  * @desc 推送信息
  * @param token
  * @param msg
  * @return
  */
 public string sendmessage(string token,string msg){
  try{
   log.info("sendmessage start.token:"+token+",msg:"+msg);
   string url = messageformat.format(this.sendmsgurl, token);
   httppost post = new httppost(url);
   responsehandler<?> responsehandler = new basicresponsehandler();
   stringentity entity = new stringentity(msg);
   post.setentity(entity);
   string response = (string) this.webclient.execute(post, responsehandler);
   log.info("return response=====start======");
   log.info(response);
   log.info("return response=====end======");
   return response;

  }catch (exception e) {
   log.error("get user info exception", e);
   return null;
  } 
 }
 /**
  * @desc 拉取用户信息
  * @param token
  * @param openid
  * @return
  */
 public weixinopenuser getuserinfo(string token, string openid) {
  try {
   log.info("getuserinfo start.{token:" + token + ",openid:" + openid + "}");
   string url = messageformat.format(this.getuserinfourl, token, openid);
   string response = executehttpget(url);
   jsonnode json = jsonutils.read(response);
   if (json.get("openid") != null) {
    weixinopenuser user = new weixinopenuser();
    user.setopenuserid(json.get("openid").astext());
    user.setstate(json.get("subscribe").astext());
    if ("1".equals(user.getstate())) {
     user.setusername(json.get("nickname").astext());
     user.setsex(json.get("sex").astext());
     user.setcity(json.get("city").astext());
     user.setlanguage(json.get("language").astext());
    }
    return user;
   }
  } catch (exception e) {
   log.error("get user info exception", e);
  }
  return null;
 }
 /**
  * @desc 发起http get请求返回数据
  * @param url
  * @return
  * @throws ioexception
  * @throws clientprotocolexception
  */
 private string executehttpget(string url) throws ioexception, clientprotocolexception {
  responsehandler<?> responsehandler = new basicresponsehandler();
  string response = (string) this.webclient.execute(new httpget(url), responsehandler);
  log.info("return response=====start======");
  log.info(response);
  log.info("return response=====end======");
  return response;
 }
}