Implementation of micro-letter Java Development Micro-letter active Push Message _java

Source: Internet
Author: User


1. Pull Access_token
2. Draw User Information
3. Push message actively
4. Interface seems to apply for permission
5. Reliance on httpclient4.2.3 and Jackson 2.2.1

Copy Code code as follows:

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.