搜尋
首頁微信小程式微信開發JAVA微信開發總結的圖文代碼分享

最近研究了一下微信,感覺不是很難,借鑒了marker(www.yl-blog.com)的開源程式碼,修改了幾處小BUG(在作者的開源網站上留了言,詳見其開源網站),很快就實現關鍵的邏輯: 
  1、開發者驗證 
  2、透過程式介面自訂選單 
  3、使用者追蹤後,發送歡迎訊息 
 4、根據使用者追蹤輸入為其選擇感興趣的主題 
  5、發送文件訊息、發送圖文訊息 
  6、使用靜態網頁模擬了微信網站的簡單模板 

  公眾號,daydayup_it,正在緊張的策劃開發中,近期上線,計畫主要提供一些優質的教育資源,敬請大家關注。 

  準備有空把關鍵技術整理一下,貼出來跟大家交流。

  一、開發者驗證,其實主要是寫個Servlet,有點Web開發經驗就很容易搞定 

package org.marker.weixin.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 处理微信服务器请求的Servlet URL地址:http://xxx/weixin/dealwith.do
 * 
 * @author marker
 * @blog www.yl-blog.com
 * @weibo http://t.qq.com/wuweiit
 */
public class WinXinServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(WinXinServlet.class);

	// TOKEN 是你在微信平台开发模式中设置的字符串
	public static final String TOKEN = "YourToken";

	/**
	 * 处理微信服务器验证
	 * http://wallimn.iteye.com, 2014-09-11
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String signature = request.getParameter("signature");// 微信加密签名
		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");// 随机数
		String echostr = request.getParameter("echostr");// 随机字符串
		Writer out = response.getWriter();
		System.out.println("收到验证请求:");
		System.out.println("  signature="+signature);
		System.out.println("  timestamp="+timestamp);
		System.out.println("  nonce="+nonce);
		System.out.println("  echostr="+echostr);
		if(signature==null || timestamp==null || nonce==null || echostr==null){
			//这几个参数为空时,排序会报错。
			out.write("parameter is null!");
		}
		else{
			// 重写totring方法,得到三个参数的拼接字符串
			List<String> list = new ArrayList<String>(3) {
				private static final long serialVersionUID = 2621444383666420433L;
				public String toString() {
					return this.get(0) + this.get(1) + this.get(2);
				}
			};
			list.add(TOKEN);
			list.add(timestamp);
			list.add(nonce);
			Collections.sort(list);// 排序
			String tmpStr = new MySecurity().encode(list.toString(),
					MySecurity.SHA_1);// SHA-1加密
			if (signature.equals(tmpStr)) {
				out.write(echostr);// 请求验证成功,返回随机码
			} else {
				out.write("check error!");
			}
		}
		out.flush();
		out.close();
	}

	
	/**
	 * 处理微信服务器发过来的各种消息,包括:文本、图片、地理位置、音乐等等
	 * http://wallimn.iteye.com, 2014-09-11
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info("收到POST请求:"+(new Date()));
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		InputStream is = request.getInputStream();
		OutputStream os = response.getOutputStream();
		//TODO:写微信平台推送过来的各种信息的处理逻辑
	}
}

  二、微信網站的測試主頁效果及程式碼,借助bootstrap,支援媒體查詢,當手機水平看時,欄會變成2*2佈局,index.jsp,需要JSTL的支持,也可以自行去掉,不影響效果,注意連接文件的路徑即可 

JAVA微信開發總結的圖文代碼分享

JAVA微信開發總結的圖文代碼分享


#

以上是JAVA微信開發總結的圖文代碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。