1。序文
WeChatインターフェイスコール検証に最終的に使用される3つのパラメーター:
次に、これらの3つのパラメーターを取得するための詳細なコードに示されます。この記事の環境はEclipse + Mavenです。
この記事で使用されているテクノロジーhttpclient、json string to map、sha1暗号化
2。必要なJARパッケージ
Mavenが依存するパッケージは次のとおりです。
1。HTTPCLIENTパッケージの依存関係
<Dependency> groupId> org.apache.httpComponents </groupId> <artifactid> httpcore </artifactid> <バージョン> 4.4.3 </version> </dependency> <seplency> <seplency> <バージョン> 4.5.1 </version> </dependency>
2。関連するパッケージの依存関係をマップするJSON
<Dependency> GroupId> net.sf.json-lib </groupid> <artifactid> json-lib </artifactid> <version> 2.4 </version> <classifier> jdk15 </classifier> </dependency> <依存症
3。操作結果
4。詳細コード
パッケージcom.luo.util; Import java.io.ioexception; Import java.io.unsupportedencodingception; import java.security.messagegest; import java.security.nosuchalgorithmexception; java.util.list; Import java.util.map; import java.util.set; import java.util.uuid; import net.sf.json.jsonobject; import org.apache.http.httpentity; Import org.apache.http.httpprespons; Import.Pair.http.httpons; org.apache.http.parseexception; Import org.apache.http.client.clientProtocolexception; Import org.apache.http.client.entity.urlencodedformentity; Import org.apache.http.client.client.methods.httpget; import apach.htppt.cltp.cltp. org.apache.http.client.methods.httpurirequest; Import org.apache.http.impl.client.defaulthttpclient; Import org.apache.http.message.basicnamevaluepair; Import org.apache.http.protocol.http; Import org.apache.http.util.entityutils; public class httpxmlclient {public static string post(string url、map <string、string> params){defaulthttpclient httpclient = new defulthttpclient();文字列body = null; httppost post = pistform(url、params); body = invoke(httpclient、post); httpclient.getConnectionManager()。shutdown();体を返す; } public static string get(string url){defaulthttpclient httpclient = new defulthttpclient();文字列body = null; httpget get = new httpget(url); body = invoke(httpclient、get); httpclient.getConnectionManager()。shutdown();体を返す; } private static string invoke(defulthttpclient httpclient、httpurirequest httppost){httpresponse応答= sendrequest(httpclient、httppost); string body = paseresponse(response);体を返す; } private static string paseresponse(httpresponse response){httpentity entity = respons.getentity(); string charset = entityutils.getContentCharset(entity);文字列body = null; try {body = entityutils.toString(entity); } catch(parseexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } return body; } private static httpresponse sendRequest(defaulthttpclient httppclient、httpurirequest httppost){httpresponse response = null; try {response = httpclient.execute(httpost); } catch(clientProtoColexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); }返信応答; } private static httppost pospform(string url、map <string、string> params){httppost httppost = new httppost(url);リスト<NameValuePair> nvps = new ArrayList <NameValuePair>(); <string> keyset = params.keyset(); for(string key:keyset){nvps.add(new BasicNameValuePair(key、params.get(key))); } try {http.setentity(new UrlencodedFormentity(nvps、http.utf_8)); } catch(unsupportedencodingexception e){e.printstacktrace(); } httpを返します。 } public static void main(string [] args){// 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); map <string、string> map = new hashmap <string、string>(); iterator <string> it = jsonmap.keys(); while(it.hasnext()){string key =(string)it.next(); string u = jsonmap.get(key).toString(); map.put(key、u); } string access_token = map.get( "access_token"); system.out.println( "access_token =" + access_token); //チケットparams.putを取得する( "access_token"、access_token); xml = httpxmlclient.post( "https://qyapi.weixin.qc.com/cgi-bin/get_jsapi_ticket"、params); jsonmap = jsonobject.fromobject(xml); map = new hashmap <string、string>();それ= jsonmap.keys(); while(it.hasnext()){string key =(string)it.next(); string u = jsonmap.get(key).toString(); map.put(key、u); } string jsapi_ticket = map.get( "チケット"); system.out.println( "jsapi_ticket =" + jsapi_ticket); //署名文字列noncestr = uuid.randomuuid()。toString()を取得します。 String Timestamp = long.toString(system.currenttimemillis() / 1000); string url = "http://mp.weixin.qq.com"; string str = "jsapi_ticket =" + jsapi_ticket + "&noncestr =" + noncestr + "×tamp =" + timestamp + "&url =" + url; // sha1暗号化文字列署名= sha1(str); System.out.println( "noncestr =" + noncestr); system.out.println( "timestamp =" + timestamp); System.out.println( "signature =" + signature); //最後に、非cestr、タイムスタンプ、wechat jsインターフェイスの確認に必要な署名}/*** @author:luo guohui* @date:2015年12月17日9:24:43 AM* @description:sha、sha1 encryption* @parameter:string* {try {Messaged Gigest Digest = java.security.messagedigest .getInstance( "sha-1"); // sha暗号化の場合は、「sha-1」を「sha」に変更してdigest.update(str.getbytes()); BYTE MESSAGEDGEST [] = Digest.Digest(); // hex string stringbuffer hexstr = new StringBuffer()を作成します。 //(int i = 0; i <messagedigest.length; i ++){string shahex = integer.tohexstring(mesagedgest [i]&0xff); if(shahex.length()<2){hexstr.append(0); } hexstr.append(shahex); } return hexstr.toString(); } catch(nosuchalgorithmexception e){e.printstacktrace(); } nullを返します。 }}5。プロジェクトダウンロード
WeChatで署名プロジェクトを入手してください
よりエキサイティングなコンテンツについては、「Android Wechat Development Tutorial Summary」と「Java Wechat Developmentチュートリアルの概要」をクリックしてください。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。