開發接入測試。
進入微信開發文檔,進入到接口號測試。
Q:爲啥要進行測試號申請系統?
A:因爲在我們個人申請的公衆號中,開發/接口權限,這一欄中可以看到,我們的權限實在太少,因此,作爲開發人員,我們需要進入測試號系統,目的是獲得更多的權限。
在進行開發之前先閱讀微信官方文檔,接入指南。https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
從上面的文檔上我們可以看出,我們先做前兩步。
1.填寫服務器配置。
在進入測試號系統之後,我們需要填寫配置信息。URL和token.
URL填寫之前申請到的網址,token這裏可以隨意填寫一些。
關於url的一些信息,在樓主的文章:微信公衆號開發-開發準備 ,一文中有詳細的說明。
2. 驗證消息的確來自微信服務器。
創建一個servlet,編寫測試代碼。
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class WxServlet
*/
@WebServlet("/wx")
public class WxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public WxServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
*
* 參數 描述
* signature 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。
* timestamp 時間戳
* nonce 隨機數
* echostr 隨機字符串
*
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
System.out.println("signature:" + signature);
System.out.println("timestamp:" + timestamp);
System.out.println("nonce:" + nonce);
System.out.println("echostr:" + echostr);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// doGet(request, response);
System.out.println("post");
}
}
然後我們再配置第一步中的URL和token.在之前配置的URL的後面拼接上我們工程的名字和代碼中@WebServlet中的名字。如下圖所示:
然後啓動項目,點擊提交。
提交之後樓主這裏遇到了一個問題。如下圖所示:
在這裏我關閉了windows definder.再次啓動測試,發現還是同樣的頁面。但是用 http://localhost:8080/wechat/wx訪問的時候卻是正常的。於是我將隧道配置中的端口號改爲了8080,居然奇蹟般的成功了。結果如下圖所示:
在這裏需要說明的是,關於隧道不可用問題,問題的原因很多,樓主這裏將端口號改成了8080之後成功了,但是有人改了端口號之後依然沒有成功,因此,樓主爲了後面開發的順利,將隧道不可用問題也就沒有再進行深入的研究和探討。 如果大家遇到了同樣的問題,但是用了不同的解決方案並且成功了的,歡迎在評論區給我留言,大家相互學習探討。