public class Weixinapihelper {
/**
* Get Token interface
*/
Private String Gettokenurl = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}" &secret={1} ";
/**
* Pull micro-letter user Information interface
*/
Private String Getuserinfourl = "Https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}";
/**
* Active Push Information interface
*/
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 Initialize the creation of 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 Obtain Authorization 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 Push information
* @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 to draw user information
* @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 initiate HTTP GET request return data
* @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;
}
}