1. Pull Access_Token
2. Ziehen Sie Benutzerinformationen an
3. Drücken Sie aktiv Nachrichten
4. Die Schnittstelle scheint die Erlaubnis zu erfordern
5. auf httpclient4.2.3 und Jackson 2.2.1 verlassen
Die Codekopie lautet wie folgt:
öffentliche Klasse Wixinapihelper {
/**
* Holen Sie sich die Token -Schnittstelle
*/
private String GetTokenURL = "https://api.weixin.qq.com/cgi-in/token?grant_type=client_credential&appid= <e0}&secret=iN1}";
/**
* Ziehen Sie die WeChat -Benutzerinformationsschnittstelle an
*/
private Zeichenfolge getUserInfourl = "https://api.weixin.qqq.com/cgi-ner/user/info?access_token= <E0}&openid=i1}";
/**
* Aktive Push -Informationsschnittstelle
*/
private String sendmsgurl = "https://api.weixin.qq.com/cgi-ner/message/send?access_token= <E0}";
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 initialisieren Sie die Erstellung von Webclient
*/
public void initwebclient () {
log.info ("initwebclient start ....");
versuchen {
PoolingClientConnectionManager TCM = New PoolingClientConnectionManager ();
tcm.setMaxtotal (10);
SslContext ctx = sslcontext.getInstance ("tls");
X509Trustmanager tm = new x509Trustmanager () {
public void checkclienttrusted (x509certificate [] arg0, String arg1) löst CertificateException {aus
}
public void CheckServertrusted (x509certificate [] arg0, String arg1) löst CertificateException {aus
}
public x509certificate [] getAcceptedIssuers () {
null zurückkehren;
}
};
ctx.init (null, neuer x509Trustmanager [] {tm}, null);
SSLSOCKETFACTORY SSF = NEU SSLSOCKETFACTORY (CTX, SSLSOCKETFACTORY.Allow_all_Hostname_verifier);
Schema Sch = neues Schema ("Https", 443, SSF);
tcm.getschemeregistry (). Register (Sch);
webclient = new Defaulthttpclient (TCM);
} catch (Ausnahme ex) {
log.Error ("Initwebclient Exception", Ex);
} Endlich {
log.info ("initwebclientend ...");
}
}
/**
* @desc bekommen autorisiertes Token
* @param Appid
* @param Secret
* @zurückkehren
*/
public String getAccessToken (String Appid, String Secret) {
String accessToken = null;
versuchen {
log.info ("GetAccessToken start. {Appid =" + Appid + ", Geheimnis:" + Geheimnis + "}");
String url = messageFormat.Format (this.getTokenUrl, Appid, Secret);
String response = executeHttpget (URL);
AccessToken = jsonUtils.read (Antwort, "access_token");
} catch (Ausnahme e) {
log.Error ("Access Toekn -Ausnahme erhalten", e);
}
AccessToken zurückgeben;
}
/**
* @Desc Push -Informationen
* @param token
* @param msg
* @zurückkehren
*/
public String sendMessage (String Token, String msg) {
versuchen{
log.info ("sendMessage start.token:"+token+", msg:"+msg);
String url = messageFormat.format (this.sendmsgurl, token);
Httppost post = new httppost (URL);
ReaktionsHandler <?> Reaktionshandler = new BasicResponseHandler ();
StrackEntity entity = new strackentity (msg);
Post.Setentity (Entität);
String response = (string) this.webclient.execute (post, reaktionsHandler);
log.info("return response======start====================);
log.info (Antwort);
log.info("return response=====end=====================);
Rücksendemantwort;
} catch (Ausnahme e) {
log.Error ("Benutzerninformationen abrufen", e);
null zurückkehren;
}
}
/**
* @Desc Benutzerinformationen ziehen
* @param token
* @param openID
* @zurückkehren
*/
public wexinopenuser getUerinfo (String Token, String OpenID) {
versuchen {
log.info ("GetUserInfo Start. {Token:" + Token + ", OpenID:" + openID + "}");
String url = messageFormat.format (this.getUserinFourl, Token, OpenID);
String response = executeHttpget (URL);
JSONNODE JSON = JSONUTILS.READ (Antwort);
if (json.get ("openID")! = null) {
WixInopenuser user = new wixinopenuser ();
user.setOpenUSerID (json.get ("openID"). AsText ());
user.setState (json.get ("abonnieren"). asText ());
if ("1" .equals (user.getState ())) {
user.setUserName (json.get ("Spitzname"). asText ());
user.setSex (json.get ("sex"). asText ());
user.setCity (json.get ("Stadt"). AsText ());
user.setLanguage (json.get ("Sprache"). asText ());
}
Benutzer zurückgeben;
}
} catch (Ausnahme e) {
log.Error ("Benutzerninformationen abrufen", e);
}
null zurückkehren;
}
/**
* @DESC initiiert die HTTP -GET -Anforderung zur Rückgabe von Daten
* @param url
* @zurückkehren
* @throws ioException
* @Throws ClientProtocolException
*/
private String executEHttpget (String url) löst IOException, ClientProtocolException {aus
ReaktionsHandler <?> Reaktionshandler = new BasicResponseHandler ();
String response = (string) this.webclient.execute (new Httpget (URL), ReaktionsHandler);
log.info("return response======start====================);
log.info (Antwort);
log.info("return response=====end=====================);
Rücksendemantwort;
}
}