真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

微信開發(fā)微信jsapi與java初步接入的示例

小編給大家分享一下微信開發(fā)微信jsapi與java初步接入的示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為南譙企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì),南譙網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

                                                       參數(shù)名

http://www.php.cn/wiki/835.html" target="_blank">width="346" valign="top" style="word-break:break-all">描述
appId應(yīng)用ID 登錄微信公眾號(hào)管理平臺(tái)可查詢
timestamp必填,生成簽名的時(shí)間戳
nonceStr必填,生成簽名的隨機(jī)串
signature必填,簽名,見附錄1

上述表格中的參數(shù),我們?cè)谇耙徽鹿?jié)已經(jīng)說的很明白,之所以做出一個(gè)表格是因?yàn)槿绻胍晒尤胛⑿舑sapi這四個(gè)參數(shù)是憑證,也就是相當(dāng)于一個(gè)門必須要有四把鑰匙才能打開,缺一不可 。

接下來的案例采用java的servlet做的跳轉(zhuǎn)頁面,沒有用到springMVC,大家可把請(qǐng)求的路徑更換成controller路徑即可。

WxJsAPIServlet代碼:

package com.test;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.test.util.JsapiTicketUtil;

import com.test.util.Sign;

public class WxJsAPIServlet extends HttpServlet {

/**

* Constructor of the object.

*/

public WxJsAPIServlet() {

super();

}

/**

* Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println("wxJSAPI====================");

String jsapi_ticket =JsapiTicketUtil.getJSApiTicket();;

Map map = Sign.sign(jsapi_ticket, "http://www.vxzsk.com/weChat/wxJsAPIServlet");

String timestamp = map.get("timestamp");

String nonceStr = map.get("nonceStr");

String signature = map.get("signature");

String appId = "應(yīng)用Id";

request.setAttribute("appId", appId);

request.setAttribute("timestamp", timestamp);

request.setAttribute("signature",signature);

request.setAttribute("nonceStr", nonceStr);

request.getRequestDispatcher("jsapi/jsapi.jsp").forward(request, response);

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doGet(request, response);

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

第44行是生成 jsapi_ticket的工具類,在下面有貼出工具類的代碼。

第45行 Sign類的sign方法,把表格中的最后三個(gè)參數(shù)封裝放到Map集合中了。其中參數(shù)就是請(qǐng)求的servlet地址并跳轉(zhuǎn)到調(diào)用微信jsapi的jsp界面。

第49行 appId替換成你自己的應(yīng)用id,如果不知道應(yīng)用id 可登陸微信公眾平臺(tái)管理中心查詢。

servlet對(duì)應(yīng)的web.xml代碼

This is the description of my J2EE component

This is the display name of my J2EE component

WxJsAPIServlet

com.test.WxJsAPIServlet

WxJsAPIServlet

/wxJsAPIServlet

生成簽名算法類Sign代碼:

package com.test.util;

/***

* V型知識(shí)庫 www.vxzsk.com

*/

import java.util.UUID;

import java.util.Map;

import java.util.HashMap;

import java.util.Formatter;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.io.UnsupportedEncodingException;

public class Sign {

public static Map sign(String jsapi_ticket, String url) {

Map ret = new HashMap();

String nonce_str = create_nonce_str();

String timestamp = create_timestamp();

String string1;

String signature = "";

//注意這里參數(shù)名必須全部小寫,且必須有序

string1 = "jsapi_ticket=" + jsapi_ticket +

"&noncestr=" + nonce_str +

"×tamp=" + timestamp +

"&url=" + url;

System.out.println(string1);

try

{

MessageDigest crypt = MessageDigest.getInstance("SHA-1");

crypt.reset();

crypt.update(string1.getBytes("UTF-8"));

signature = byteToHex(crypt.digest());

}

catch (NoSuchAlgorithmException e)

{

e.printStackTrace();

}

catch (UnsupportedEncodingException e)

{

e.printStackTrace();

}

ret.put("url", url);

ret.put("jsapi_ticket", jsapi_ticket);

ret.put("nonceStr", nonce_str);

ret.put("timestamp", timestamp);

ret.put("signature", signature);

return ret;

}

private static String byteToHex(final byte[] hash) {

Formatter formatter = new Formatter();

for (byte b : hash)

{

formatter.format("%02x", b);

}

String result = formatter.toString();

formatter.close();

return result;

}

private static String create_nonce_str() {

return UUID.randomUUID().toString();

}

private static String create_timestamp() {

return Long.toString(System.currentTimeMillis() / 1000);

}

public static void main(String[] args) {

String jsapi_ticket =JsapiTicketUtil.getJSApiTicket();

// 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode

String url = "http://www.vxzsk.com/xx/x.do";//url是你請(qǐng)求的一個(gè)action或者controller地址,并且方法直接跳轉(zhuǎn)到使用jsapi的jsp界面

Map ret = sign(jsapi_ticket, url);

for (Map.Entry entry : ret.entrySet()) {

System.out.println(entry.getKey() + ", " + entry.getValue());

}

};

}

生成jsapi_ticket參數(shù)的工具類JsapiTicketUtil代碼

package com.test.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import net.sf.json.JSONObject;

import com.test.weixin.TestAcessToken;

public class JsapiTicketUtil {

/***

* 模擬get請(qǐng)求

* @param url

* @param charset

* @param timeout

* @return

*/

public static String sendGet(String url, String charset, int timeout)

{

String result = "";

try

{

URL u = new URL(url);

try

{

URLConnection conn = u.openConnection();

conn.connect();

conn.setConnectTimeout(timeout);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));

String line="";

while ((line = in.readLine()) != null)

{

result = result + line;

}

in.close();

} catch (IOException e) {

return result;

}

}

catch (MalformedURLException e)

{

return result;

}

return result;

}

public static String getAccessToken(){

String appid="你公眾號(hào)基本設(shè)置里的應(yīng)用id";//應(yīng)用ID

String appSecret="你公眾號(hào)基本設(shè)置里的應(yīng)用密鑰";//(應(yīng)用密鑰)

String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+"";

String backData=TestAcessToken.sendGet(url, "utf-8", 10000);

String accessToken = (String) JSONObject.fromObject(backData).get("access_token");

return accessToken;

}

public static String getJSApiTicket(){

//獲取token

String acess_token= JsapiTicketUtil.getAccessToken();

String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+acess_token+"&type=jsapi";

String backData=TestAcessToken.sendGet(urlStr, "utf-8", 10000);

String ticket = (String) JSONObject.fromObject(backData).get("ticket");

return  ticket;

}

public static void main(String[] args) {

String jsapiTicket = JsapiTicketUtil.getJSApiTicket();

System.out.println("調(diào)用微信jsapi的憑證票為:"+jsapiTicket);

}

}

上述代碼中有個(gè)獲取access_token的方法,請(qǐng)讀者更換自己的參數(shù)即可

jsapi.jsp代碼

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

">

微信jsapi測試-V型知識(shí)庫

歡迎來到微信jsapi測試界面-V型知識(shí)庫


timestamp:${ timestamp}

nonceStr:${ nonceStr}

signature:${ signature}

appId:${ appId}



測試場景:打開微信公眾號(hào),點(diǎn)擊菜單回復(fù)帶有請(qǐng)求servlet地址,跳轉(zhuǎn)到j(luò)sapi.jsp界面鏈接地址,然后界面會(huì)彈出調(diào)用微信jsapi成功或失敗的窗口信息,所以還需要接下來的代碼:

WeChatServlet為微信接入的servlet,不清楚的同學(xué)可學(xué)習(xí)我們的微信開發(fā)教程。

package com.test;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.test.message.resp.TextMessage;

import com.test.util.MessageUtil;

/**

* 核心請(qǐng)求處理類

* doGet方法里 有個(gè)weixinTest,這個(gè)是公眾管理平臺(tái)里面自己設(shè)置的token 大家根據(jù)自己的token替換

*/

public class WeChatServlet extends HttpServlet {

private static final long serialVersionUID = 1508798736675904038L;

/**

* 確認(rèn)請(qǐng)求來自微信服務(wù)器

*/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("V型知識(shí)庫原創(chuàng)www.vxzsk.com");

// 微信加密簽名

String signature = request.getParameter("signature");

System.out.println("微信加密簽名signature:-----------------------"+signature);

// 時(shí)間戳

String timestamp = request.getParameter("timestamp");

System.out.println("時(shí)間戳timestamp:-----------------------"+timestamp);

// 隨機(jī)數(shù)

String nonce = request.getParameter("nonce");

System.out.println("隨機(jī)數(shù)nonce:-----------------------"+nonce);

// 隨機(jī)字符串

String echostr = request.getParameter("echostr");

System.out.println("隨機(jī)字符串echostr:-----------------------"+echostr);

//System.out.println("token-----------------------:"+token);

PrintWriter out = response.getWriter();

// 通過檢驗(yàn)signature對(duì)請(qǐng)求進(jìn)行校驗(yàn),若校驗(yàn)成功則原樣返回echostr,表示接入成功,否則接入失敗

if (SignUtil.checkSignature("weixinTest", signature, timestamp, nonce)) {

out.print(echostr);

//System.out.println("這是:"+echostr);

}

out.close();

out = null;

}

/**

* 處理微信服務(wù)器發(fā)來的消息

* 實(shí)例源碼在文章頂部有下載連接

*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("V型知識(shí)庫原創(chuàng)www.vxzsk.com");

System.out.println("微信服務(wù)器發(fā)來消息------------");

// 將請(qǐng)求、響應(yīng)的編碼均設(shè)置為UTF-8(防止中文亂碼)

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

String respMessage = null;

try{

//xml請(qǐng)求解析

Map requestMap = MessageUtil.parseXml(request);//接收微信發(fā)過來的xml格式

//發(fā)送方帳號(hào)(open_id)

String fromUserName = requestMap.get("FromUserName");

//公眾帳號(hào)

String toUserName = requestMap.get("ToUserName");

//消息類型

String msgType = requestMap.get("MsgType");

//消息創(chuàng)建時(shí)間

String createTime = requestMap.get("CreateTime");

//微信服務(wù)器post過來的內(nèi)容

String weixinContent = requestMap.get("Content");

System.out.println("公眾號(hào)用戶發(fā)送過來的文本消息內(nèi)容:"+weixinContent);

//接下來我們用上一章節(jié)自己封裝好的工具類

if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {//文本類型 用戶回復(fù) “hh” 微信自動(dòng)回復(fù)此條消息

//回復(fù)換行的文本消息

TextMessage textMessage = new TextMessage();

textMessage.setToUserName(fromUserName);

textMessage.setFromUserName(toUserName);

textMessage.setCreateTime(new Date().getTime());

textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);

textMessage.setFuncFlag(0);

//回復(fù)用戶的換行字符串 \n表示換行

StringBuffer buffer = new StringBuffer();

if(weixinContent.equals("hh")){//如果用戶發(fā)送”hh“

buffer.append("歡迎訪問").append("\n");

buffer.append("微信jsapi測試界面").append("\n\n");

buffer.append("回復(fù)'hh'二字即可能顯示此條消息");

}else{

buffer.append("您好我是V型知識(shí)庫");

}

textMessage.setContent(buffer.toString());

respMessage = MessageUtil.textMessageToXml(textMessage);//轉(zhuǎn)換成xml格式

}

// 響應(yīng)回復(fù)消息

PrintWriter out = response.getWriter();

out.print(respMessage);

out.close();

}catch(Exception e){

e.printStackTrace();

}

}

}

MessageUtil工具類

package com.test.util;

import java.io.InputStream;

import java.io.Writer;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import com.test.message.resp.Article;

import com.test.message.resp.MusicMessage;

import com.test.message.resp.NewsMessage;

import com.test.message.resp.TextMessage;

import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.core.util.QuickWriter;

import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;

import com.thoughtworks.xstream.io.xml.XppDriver;

/**

* 消息工具類

*/

public class MessageUtil {

/**

* 返回消息類型:文本

*/

public static final String RESP_MESSAGE_TYPE_TEXT = "text";

/**

* 返回消息類型:音樂

*/

public static final String RESP_MESSAGE_TYPE_MUSIC = "music";

/**

* 返回消息類型:圖文

*/

public static final String RESP_MESSAGE_TYPE_NEWS = "news";

/**

* 請(qǐng)求消息類型:文本

*/

public static final String REQ_MESSAGE_TYPE_TEXT = "text";

/**

* 請(qǐng)求消息類型:圖片

*/

public static final String REQ_MESSAGE_TYPE_IMAGE = "image";

/**

* 請(qǐng)求消息類型:鏈接

*/

public static final String REQ_MESSAGE_TYPE_LINK = "link";

/**

* 請(qǐng)求消息類型:地理位置

*/

public static final String REQ_MESSAGE_TYPE_LOCATION = "location";

/**

* 請(qǐng)求消息類型:音頻

*/

public static final String REQ_MESSAGE_TYPE_VOICE = "voice";

/**

* 請(qǐng)求消息類型:推送

*/

public static final String REQ_MESSAGE_TYPE_EVENT = "event";

/**

* 事件類型:subscribe(訂閱)

*/

public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";

/**

* 事件類型:unsubscribe(取消訂閱)

*/

public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";

/**

* 事件類型:CLICK(自定義菜單點(diǎn)擊事件)

*/

public static final String EVENT_TYPE_CLICK = "CLICK";

/**

* 解析微信發(fā)來的請(qǐng)求(XML)

*

* @param request

* @return

* @throws Exception

*/

@SuppressWarnings("unchecked")

public static Map parseXml(HttpServletRequest request) throws Exception {

// 將解析結(jié)果存儲(chǔ)在HashMap中

Map map = new HashMap();

// 從request中取得輸入流

InputStream inputStream = request.getInputStream();

// 讀取輸入流

SAXReader reader = new SAXReader();

Document document = reader.read(inputStream);

// 得到xml根元素

Element root = document.getRootElement();

// 得到根元素的所有子節(jié)點(diǎn)

List elementList = root.elements();

// 遍歷所有子節(jié)點(diǎn)

for (Element e : elementList) {

map.put(e.getName(), e.getText());

}

// 釋放資源

inputStream.close();

inputStream = null;

return map;

}

/**

* 文本消息對(duì)象轉(zhuǎn)換成xml

* @param textMessage 文本消息對(duì)象

* @return xml

*/

public static String textMessageToXml(TextMessage textMessage) {

xstream.alias("xml", textMessage.getClass());

return xstream.toXML(textMessage);

}

/**

* 音樂消息對(duì)象轉(zhuǎn)換成xml

* @param musicMessage 音樂消息對(duì)象

* @return xml

*/

public static String musicMessageToXml(MusicMessage musicMessage) {

xstream.alias("xml", musicMessage.getClass());

return xstream.toXML(musicMessage);

}

/**

* 圖文消息對(duì)象轉(zhuǎn)換成xml

* @param newsMessage 圖文消息對(duì)象

* @return xml

*/

public static String newsMessageToXml(NewsMessage newsMessage) {

xstream.alias("xml", newsMessage.getClass());

xstream.alias("item", new Article().getClass());

return xstream.toXML(newsMessage);

}

/**

* 擴(kuò)展xstream,使其支持CDATA塊

* @date

*/

private static XStream xstream = new XStream(new XppDriver() {

public HierarchicalStreamWriter createWriter(Writer out) {

return new PrettyPrintWriter(out) {

// 對(duì)所有xml節(jié)點(diǎn)的轉(zhuǎn)換都增加CDATA標(biāo)記

boolean cdata = true;

@SuppressWarnings("unchecked")

public void startNode(String name, Class clazz) {

super.startNode(name, clazz);

}

protected void writeText(QuickWriter writer, String text) {

if (cdata) {

writer.write("

writer.write(text);

writer.write("]]>");

} else {

writer.write(text);

}

}

};

}

});

}

TextMessage代碼

package com.test.message.resp;

public class TextMessage extends BaseMessage {

// 回復(fù)的消息內(nèi)容

private String Content;

public String getContent() {

return Content;

}

public void setContent(String content) {

Content = content;

}

}

BaseMessage代碼

package com.test.message.resp;

/**

* 消息基類(公眾帳號(hào) -> 普通用戶)

*/

public class BaseMessage {

// 接收方帳號(hào)(收到的OpenID)

private String ToUserName;

// 開發(fā)者微信號(hào)

private String FromUserName;

// 消息創(chuàng)建時(shí)間 (整型)

private long CreateTime;

// 消息類型(text/music/news)

private String MsgType;

// 位0x0001被標(biāo)志時(shí),星標(biāo)剛收到的消息

private int FuncFlag;

public String getToUserName() {

return ToUserName;

}

public void setToUserName(String toUserName) {

ToUserName = toUserName;

}

public String getFromUserName() {

return FromUserName;

}

public void setFromUserName(String fromUserName) {

FromUserName = fromUserName;

}

public long getCreateTime() {

return CreateTime;

}

public void setCreateTime(long createTime) {

CreateTime = createTime;

}

public String getMsgType() {

return MsgType;

}

public void setMsgType(String msgType) {

MsgType = msgType;

}

public int getFuncFlag() {

return FuncFlag;

}

public void setFuncFlag(int funcFlag) {

FuncFlag = funcFlag;

}

}

效果如下:

微信開發(fā)微信jsapi與java初步接入的示例

微信開發(fā)微信jsapi與java初步接入的示例

微信開發(fā)微信jsapi與java初步接入的示例

微信開發(fā)微信jsapi與java初步接入的示例速云其它相關(guān)

看完了這篇文章,相信你對(duì)“微信開發(fā)微信jsapi與java初步接入的示例”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!


網(wǎng)站名稱:微信開發(fā)微信jsapi與java初步接入的示例
鏈接地址:http://weahome.cn/article/iegedc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部