1. Pull Access_token
2. Tirez les informations de l'utilisateur
3. Pousser activement les messages
4. L'interface semble nécessiter la permission
5. S'appuyant sur HttpClient4.2.3 et Jackson 2.2.1
La copie de code est la suivante:
classe publique Weixinapihelper {
/ **
* Obtenez l'interface de jeton
* /
chaîne privée getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0 }&secret={1}";
/ **
* Tirez l'interface d'informations utilisateur WeChat
* /
chaîne privée getUserInfourl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0 }&openID={1}";
/ **
* Interface d'information push active
* /
chaîne privée sendmsgurl = "https://api.weixin.qq.com/cgi-bin/mesage/send?access_token={0}";
webClient httpclient privé;
Log privé 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 initialise la création de webclient
* /
public void initwebclient () {
log.info ("initwebclient start ....");
essayer {
PooringClientConnectionManager TCM = new PooringClientConnectionManager ();
tcm.setMextotal (10);
Sslcontext ctx = sslcontext.getInstance ("tls");
X509TrustManager tm = new x509TrustManager () {
public void CheckClientTrust (x509Certificate [] Arg0, String Arg1) lève CertificateException {
}
public void CheckServerTruted (x509Certificate [] Arg0, String Arg1) lève CertificateException {
}
public x509certificate [] getACceptEdISSUers () {
retourner null;
}
};
ctx.init (null, nouveau x509TrustManager [] {tm}, null);
Sslsocketfactory ssf = new sslsocketfactory (ctx, sslsocketfactory.allow_all_hostname_verifier);
Schéma sch = nouveau schéma ("https", 443, SSF);
tcm.getschemeRegistry (). Registre (sch);
webClient = new DefaulthTTPClient (TCM);
} catch (exception ex) {
Log.Error ("InitWebClient Exception", ex);
} enfin {
log.info ("initwebclient end ...");
}
}
/ **
* @desc obtient un jeton autorisé
* @param appid
* @param secret
* @retour
* /
public String getAccessToken (String Appid, String Secret) {
String AccessToken = null;
essayer {
log.info ("getAccessToken start. {appid =" + appid + ", secret:" + secret + "}");
String url = MessageFormat.format (this.getTokenUrl, appid, secret);
String Response = EXECUTEHTTPGET (URL);
AccessToken = JSonUtils.Read (réponse, "Access_token");
} catch (exception e) {
Log.Error ("Get Access TooKN Exception", E);
}
return AccessToken;
}
/ **
* @DESC Push Information
* Token @param
* @param msg
* @retour
* /
public String SendMessage (String Token, String msg) {
essayer{
log.info ("SendMessage start.token:" + token + ", msg:" + msg);
String url = MessageFormat.format (this.sendmsgurl, token);
HTTPPOST POST = NOUVEAU HTTPPOST (URL);
RéponseHandler <?> RéponseHandler = new BasicResponseHandler ();
Entité strictetity = new stritNtity (msg);
post.SetEntity (entité);
String Response = (String) this.webClient.Execute (Post, ResponseHandler);
Log.info ("Retour Response ===== start ===================);
Log.info (réponse);
Log.info ("Retour Response ===== end ====================);
réponse de retour;
} catch (exception e) {
Log.Error ("Obtenir l'exception des informations utilisateur", e);
retourner null;
}
}
/ **
* @DESC tire les informations utilisateur
* Token @param
* @param openID
* @retour
* /
public weixinopenser getUserIrinfo (String token, String openId) {
essayer {
log.info ("getUserInfo start. {token:" + token + ", openId:" + openId + "}");
String url = MessageFormat.Format (this.getUserIrinfourl, token, openId);
String Response = EXECUTEHTTPGET (URL);
JSONNODE JSON = JSONUTILS.Read (Response);
if (json.get ("openId")! = null) {
WeixinOpenSer user = new WeixinOpenSer ();
user.setOpenUserId (json.get ("openId"). AsText ());
user.setState (json.get ("abonnez-vous"). asText ());
if ("1" .equals (user.getState ())) {
user.setUsername (json.get ("surnom"). asText ());
user.setSex (json.get ("sexe"). AsText ());
user.setCity (json.get ("ville"). AsText ());
user.setLanguage (JSON.get ("Language"). AsText ());
}
RETOUR UTILISATEUR;
}
} catch (exception e) {
Log.Error ("Obtenir l'exception des informations utilisateur", e);
}
retourner null;
}
/ **
* @DESC lance HTTP GET Demande pour retourner les données
* URL @param
* @retour
* @throws ioexception
* @throws clientProtocolexception
* /
Private String executeHttpget (URL de chaîne) lève ioException, clientProtoCoLexception {
RéponseHandler <?> RéponseHandler = new BasicResponseHandler ();
String Response = (String) this.webClient.Execute (nouveau httpget (URL), réponse);
Log.info ("Retour Response ====== start ===================);
Log.info (réponse);
Log.info ("Retour Response ===== end ====================);
réponse de retour;
}
}