1. مقدمة
المعلمات الثلاثة التي يتم استخدامها في النهاية للتحقق من مكالمة واجهة WeChat:
بعد ذلك ، سيتم إعطاء الكود التفصيلي للحصول على هذه المعلمات الثلاثة. بيئة هذه المقالة هي Eclipse + Maven.
التقنيات المستخدمة في هذه المقالة
2. حزمة الجرة المطلوبة
الحزم التي يعتمد عليها Maven هي:
1. تبعية حزمة httpclient
<Rependency> <roupeD> org.apache.httpcomponents </rougiD> <intifactid> httpcore </intifactid> <splection> 4.4.3 </version> </sependency> <redency> <rougiD> org.apache.httpcomponents </groupid> <splect> 4.5.1 </version> </sependency>
2. JSON لتعيين تبعيات الحزمة ذات الصلة
<Rependency> <roupend> net.sf.json-lib </rougiD> <StifactId> json-lib </shintifactid> <الإصدار> 2.4 </version> <slassifier> jdk15 </slasiFier> </sperency> </dependency> <sropency> xom </groupid> </attifactid>
3. نتائج التشغيل
4. رمز مفصل
package com.luo.util ؛ import java.io.ioException ؛ import java.io.unsupportedEncodingexception ؛ import java.security.messageStedIgest ؛ import java.security.nosuchalgorithmexception ؛ import java.util.ArayList ؛ java.util.list ؛ استيراد java.util.map ؛ استيراد java.util.set ؛ استيراد java.util.uuid ؛ استيراد net.sf.json.jsonoBject ؛ import org.apache.http.httpentity ؛ import org.http.htp.htpresponse ؛ org.apache.http.parseException ؛ استيراد org.apache.http.client.clientprotocolexception ؛ استيراد org.apache.http.client.entity.urlencodedformentity ؛ org.apache.http.client.methods.httpurirequest ؛ import org.apache.http.impl.client.defaulthttpclient ؛ import org.apache.http.message.basicnameValuepair ؛ org.apache.http.util.entityUtils ؛ الفئة العامة httpxmlclient {public static string post (url url string ، map <string ، string> params) {defaulthttpclient httpclient = new defaulthttpclient () ؛ سلسلة السلسلة = فارغة ؛ httppost post = postform (url ، params) ؛ الجسم = استدعاء (httpclient ، post) ؛ httpclient.getConnectionManager (). stapdown () ؛ إرجاع الجسم } السلسلة الثابتة العامة GET (url url) {defaulthttpclient httpclient = new DefaulThTtpClient () ؛ سلسلة السلسلة = فارغة ؛ httpget get = new httpget (url) ؛ الجسم = استدعاء (httpclient ، get) ؛ httpclient.getConnectionManager (). stapdown () ؛ إرجاع الجسم } سلسلة ثابتة خاصة (defaulthttpclient httpclient ، httpurirequest httppost) {httpresponse response = sendRequest (httpclient ، httppost) ؛ سلسلة السلسلة = paseresponse (استجابة) ؛ إرجاع الجسم } سلسلة ثابتة paseresponse (استجابة httpresponse) {httpentity intity = response.getentity () ؛ String charset = entityUtils.getContentCharset (الكيان) ؛ سلسلة السلسلة = فارغة ؛ جرب {body = entityUtils.ToString (الكيان) ؛ } catch (parseException e) {E.PrintStackTrace () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } إعادة الجسم ؛ } static httpresponse sendRequest (defaulthttpclient httppclient ، httpurirequest httppost) {httpresponse response = null ؛ حاول {reponse = httpclient.execute (httpost) ؛ } catch (clientProtocolexception e) {E.PrintStackTrace () ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } استجابة العودة ؛ } postform postform postform static ثابت (url سلسلة ، خريطة <سلسلة ، سلسلة> params) {httppost httppost = new httppost (url) ؛ قائمة <MameValUepair> nvps = new ArrayList <MameValUepair> () ؛ SET <STRING> KEYSET = PARAMS.KEYSET () ؛ لـ (مفتاح السلسلة: KeySet) {nvps.add (new BasicNameValUepair (Key ، params.get (key))) ؛ } جرب {http.setentity (urlencodedformentity (nvps ، http.utf_8)) ؛ } catch (UnsupportedEncodingException e) {E.PrintStackTrace () ؛ } إرجاع http ؛ } main static void main (string [] args) {// get access_token map <string ، string> params = new hashmap <string ، string> () ؛ params.put ("corpid" ، "wx5f24fa0db1819ea2") ؛ params.put ("CorpSecret" ، "UQTWZF0BQTL2KRHX0AMEKJPQ8L0AO96LSPSNFCTOBLRBUYPO4DUBHMN0_V2JHS-9") ؛ String xml = httpxmlclient.post ("https://qyapi.weixin.qq.com/cgi-bin/gettoken" ، params) ؛ jsonobject jsonmap = jsonobject.fromObject (xml) ؛ خريطة <string ، string> map = new hashmap <string ، string> () ؛ iterator <string> it = jsonmap.keys () ؛ بينما (it.hasnext ()) {string key = (string) it.next () ؛ string u = jsonmap.get (key) .toString () ؛ map.put (مفتاح ، u) ؛ } string access_token = map.get ("access_token") ؛ system.out.println ("access_token =" + access_token) ؛ // الحصول على ticket params.put ("Access_token" ، Access_Token) ؛ xml = httpxmlclient.post ("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket" ، params) ؛ jsonmap = jsonobject.fromObject (xml) ؛ MAP = new HashMap <string ، string> () ؛ it = jsonmap.keys () ؛ بينما (it.hasnext ()) {string key = (string) it.next () ؛ string u = jsonmap.get (key) .toString () ؛ map.put (مفتاح ، u) ؛ } string jsapi_ticket = map.get ("ticket") ؛ system.out.println ("jsapi_ticket =" + jsapi_ticket) ؛ // الحصول على توقيع سلسلة التوقيع noncestr = uuid.randomuuid (). toString () ؛ Tring timestamp = long.toString (System.CurrentTimeMillis () / 1000) ؛ url url = "http://mp.weixin.qq.com" ؛ String str = "jsapi_ticket =" + jsapi_ticket + "& noncestr =" + noncester + "× tamp =" + timestamp + "& url =" + url ؛ // sha1 تشفير سلسلة التوقيع = sha1 (str) ؛ System.out.println ("noncestr =" + noncestr) ؛ system.out.println ("timestamp =" + timestamp) ؛ system.out.println ("signature =" + signature) ؛ // أخيرًا ، noncestr ، timestamp ، التوقيع المطلوب للاتصال بـ wechat js واجهة التحقق}/*** author: luo guohui* date: 17 ديسمبر 2015 في 9:24:43 AM* description: sha ، charption* parameter: string: {try {messagedigest digest = java.security.messagedigest .getInstance ("sha-1") ؛ // إذا كان تشفير SHA ، فما عليك سوى تغيير "sha-1" إلى "sha" إلى digest.update (str.getbytes ()) ؛ byte messagedigest [] = digest.digest () ؛ // إنشاء hex string stringbuffer hexstr = new StringBuffer () ؛ // تحويل صفيف البايت إلى الرقم السداسي ل (int i = 0 ؛ i <messagedigest.length ؛ i ++) {String Shahex = Integer.ToHexString (messagedigest [i] & 0xff) ؛ if (shahex.length () <2) {hexstr.append (0) ؛ } hexstr.append (shahex) ؛ } إرجاع hexstr.toString () ؛ } catch (nosuchalgorithMexception e) {e.printstacktrace () ؛ } إرجاع فارغ ؛ }}5. تنزيل المشروع
الحصول على مشروع التوقيع على WeChat
لمزيد من المحتوى المثير ، يرجى النقر فوق "ملخص برنامج تعليمي Development Android WeChat" و "ملخص تعليمي Java WeChat Development".
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.