1. pull access_token
2. ดึงข้อมูลผู้ใช้
3. ส่งข้อความอย่างแข็งขัน
4. อินเทอร์เฟซดูเหมือนจะต้องได้รับอนุญาต
5. อาศัย httpClient4.2.3 และ Jackson 2.2.1
การคัดลอกรหัสมีดังนี้:
คลาสสาธารณะ Weixinapihelper {
-
* รับอินเทอร์เฟซโทเค็น
-
สตริงส่วนตัว getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= {0}&secret= {1}";
-
* ดึงอินเทอร์เฟซข้อมูลผู้ใช้ WeChat
-
สตริงส่วนตัว getUserInFourl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token= {0}&openid= {1}";
-
* อินเตอร์เฟสข้อมูลพุชที่ใช้งานอยู่
-
สตริงส่วนตัว sendmsgurl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token= {0}";
httpClient ส่วนตัว;
บันทึกส่วนตัวบันทึก = 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, พร็อกซี);
-
-
-
* @desc เริ่มต้นการสร้าง weblient
-
public void initwebclient () {
log.info ("initwebclient start .... ");
พยายาม {
PoolingClientConnectionManager TCM = ใหม่ PoolingClientConnectionManager ();
TCM.SetMaxtotal (10);
sslContext ctx = sslContext.getInstance ("TLS");
X509TrustManager tm = ใหม่ x509trustManager () {
โมฆะสาธารณะ CheckClientTrusted (X509Certificate [] Arg0, String arg1) โยนใบรับรองการรับรู้ {
-
Public Void CheckserverTrusted (X509Certificate [] Arg0, String arg1) โยนใบรับรองการรับรู้ {
-
สาธารณะ x509Certificate [] getacceptedissuers () {
คืนค่า null;
-
-
ctx.init (null, ใหม่ x509trustManager [] {tm}, null);
SSLSocketFactory SSF = ใหม่ SSLSocketFactory (CTX, SSLSocketFactory.allow_all_hostname_Verifier);
SCHEME SCH = โครงการใหม่ ("HTTPS", 443, SSF);
tcm.getschemeregistry (). register (sch);
WebClient = ใหม่ defaulthttpClient (TCM);
} catch (exception ex) {
log.error ("ข้อยกเว้น initwebclient", ex);
} ในที่สุด {
log.info ("จุดเริ่มต้นเริ่มต้น ... ");
-
-
-
* @desc ได้รับโทเค็นที่ได้รับอนุญาต
* @param appid
* @param Secret
* @กลับ
-
สตริงสาธารณะ getAccessToken (สตริง appid, สตริงลับ) {
สตริง accessToken = null;
พยายาม {
log.info ("getAccessToken start. {appid =" + appid + ", ความลับ:" + secret + "}");
string url = messageFormat.format (this.getTokenUrl, appid, Secret);
การตอบสนองสตริง = ExecuteHttPget (URL);
accessToken = jsonutils.Read (การตอบสนอง, "access_token");
} catch (Exception e) {
log.error ("รับการเข้าถึงข้อยกเว้น toekn", e);
-
return AccessToken;
-
-
* @desc ข้อมูลผลักดัน
* @param โทเค็น
* @param msg
* @กลับ
-
สตริงสาธารณะ sendMessage (โทเค็นสตริง, สตริงข้อความ) {
พยายาม{
log.info ("SendMessage start.token:"+token+", msg:"+msg);
string url = messageFormat.format (this.sendmsgurl, โทเค็น);
httppost post = new httppost (url);
ResponseHandler <?> ResponseHandler = ใหม่ BasicResponseHandler ();
strentEntity entity = new StressEntity (msg);
post.setEntity (เอนทิตี);
การตอบสนองสตริง = (สตริง) this.webclient.execute (โพสต์, responsehandler);
log.info ("การตอบกลับส่งคืน ====== เริ่ม ======================);
log.info (การตอบสนอง);
log.info ("การตอบกลับส่งคืน ===== end =======================);
การตอบกลับกลับ;
} catch (Exception e) {
log.error ("รับข้อยกเว้นข้อมูลผู้ใช้", e);
คืนค่า null;
-
-
-
* @desc ดึงข้อมูลผู้ใช้
* @param โทเค็น
* @param openid
* @กลับ
-
public weixinopenuser getUserInfo (โทเค็นสตริง, สตริง openId) {
พยายาม {
log.info ("getUserInfo start. {โทเค็น:" + โทเค็น + ", openId:" + openId + "}");
string url = messageFormat.format (this.getUserInfourl, โทเค็น, openId);
การตอบสนองสตริง = ExecuteHttPget (URL);
jsonNode json = jsonutils.read (การตอบกลับ);
if (json.get ("openId")! = null) {
ผู้ใช้ Weixinopenuser = ใหม่ weixinopenuser ();
user.setOpenUserId (json.get ("openId"). astext ());
user.setState (json.get ("สมัครสมาชิก"). astext ());
if ("1" .Equals (user.getState ())) {
user.setUserName (json.get ("ชื่อเล่น"). astext ());
user.setsex (json.get ("เพศ"). astext ());
user.setCity (json.get ("เมือง"). astext ());
user.setLanguage (json.get ("ภาษา"). astext ());
-
ผู้ใช้ส่งคืน;
-
} catch (Exception e) {
log.error ("รับข้อยกเว้นข้อมูลผู้ใช้", e);
-
คืนค่า null;
-
-
* @DESC เริ่มต้น HTTP GET Request to return data
* @param url
* @กลับ
* @throws ioexception
* @throws clientprotocolexception
-
สตริงส่วนตัว Executehttpget (url string) พ่น ioexception, clientprotocolexception {
ResponseHandler <?> ResponseHandler = ใหม่ BasicResponseHandler ();
การตอบสนองสตริง = (สตริง) this.webclient.execute (ใหม่ httpget (url), responsehandler);
log.info ("การตอบกลับส่งคืน ====== เริ่ม ======================);
log.info (การตอบสนอง);
log.info ("การตอบกลับส่งคืน ===== end =======================);
การตอบกลับกลับ;
-
-