微信開發第一步 : 配置API,實現接收消息服務配置
1.在下圖界面先填好內容,事件消息處理可自行選擇,我這里是沒處理的
2.第二步: 使用vs 進行代碼的編寫,以下是我的代碼。LogTextHelper類需要自己寫的,微信官方文檔下載地址 https://work.weixin.qq.com/api/doc#10128 ,里面有多種版本,可自行選擇。
/// <summary> /// 處理企業號的信息 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { byte[] btHtml = context.Request.BinaryRead(context.Request.ContentLength); string strHtml = System.Text.Encoding.Default.GetString(btHtml); LogTextHelper.WriteLogFile(context.Request.Url.ToString() + "\r\n" + strHtml); string postString = string.Empty; if (context.Request.HttpMethod.ToUpper() == "GET") { Auth(); } //POST } /// <summary> /// 成為開發者的第一步,驗證並相應服務器的數據 /// </summary> private void Auth() { LogTextHelper LogHelper = new LogTextHelper(); string token = ConfigurationManager.AppSettings["CorpToken"];//從配置文件獲取Token string encodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];//從配置文件獲取EncodingAESKey string corpId = ConfigurationManager.AppSettings["CorpId"];//從配置文件獲取corpId string echoStringinitial = HttpContext.Current.Request.QueryString["echostr"]; string echoString = HttpUtility.UrlDecode(echoStringinitial); // 開發文檔中寫的“企業在獲取時需要做urldecode處理” string signature = HttpContext.Current.Request.QueryString["msg_signature"]; string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"]; string decryptEchoString = ""; if (CheckSignature(token, signature, timestamp, nonce, corpId, encodingAESKey, echoString, ref decryptEchoString)) { if (!string.IsNullOrEmpty(decryptEchoString)) { HttpContext.Current.Response.Write(decryptEchoString); HttpContext.Current.Response.End(); } } } /// <summary> /// 驗證企業號簽名 /// </summary> /// <param name="token">企業號配置的Token</param> /// <param name="signature">簽名內容</param> /// <param name="timestamp">時間戳</param> /// <param name="nonce">nonce參數</param> /// <param name="corpId">企業號ID標識</param> /// <param name="encodingAESKey">加密鍵</param> /// <param name="echostr">內容字符串</param> /// <param name="retEchostr">返回的字符串</param> /// <returns></returns> public bool CheckSignature(string token, string signature, string timestamp, string nonce, string corpId, string encodingAESKey, string echostr, ref string retEchostr) { WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId); int ret=wxcpt.VerifyURL(signature,timestamp,nonce,echostr,ref retEchostr); if (ret != 0) { //LogTextHelper.Error("ERR: VerifyURL fail, ret: " + ret); return false; } // HttpContext.Current.Response.Write(retEchostr); return true; //ret==0表示驗證成功,retEchostr參數表示明文,用戶需要將retEchostr作為get請求的返回參數,返回給企業號。 //HttpUtils.SetResponse(retEchostr); } public bool IsReusable { get { return false; } } }
3.第三步: 導入微信官方加解密文件,之后注意 token ,corpid ,encodingAESKey 的值保持一致 。 還有一點就是要注意,cshap 庫要下載最新版,老版是沒有 VerifyURL() 這個方法的。 這是下載地址 : https://work.weixin.qq.com/api/doc#10128
<appSettings> <!--企業號配置信息--> <add key="CorpToken" value="自己所填的token"/> <add key="CorpId" value="自己企業號的corpid"/> <add key="EncodingAESKey" value="自己所填的加密鍵"/> </appSettings>
4.將所寫的 web應用程序發布到 FTP 上 ,在回到 服務器配置那 ,點擊保存,第一次點擊會出現 回調URL失敗 ,這是因為值還沒傳過來 。 之后你寫的日志文件里就會多個Log ,將里面的值返回到web應用程序里。 如下圖:
這微信服務器發過來的參數 ,Log 里面有。
5.這是LogTextHelper.cs 類
public class LogTextHelper { // 創建日志 ///----------------------------------------------------------------------------- /// <summary>創建錯誤日志 在c:\ErrorLog\</summary> /// <param name="message">記錄信息 /// <returns></returns> ///----------------------------------------------------------------------------- public static void WriteLogFile(string message) { string strPath; //文件的路徑 DateTime dt = DateTime.Now; try { strPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Log"); //winform工程\bin\目錄下 創建日志文件夾 if (Directory.Exists(strPath) == false) //工程目錄下 Log目錄 '目錄是否存在,為true則沒有此目錄 { Directory.CreateDirectory(strPath); //建立目錄 Directory為目錄對象 } strPath = strPath + "\\" + dt.ToString("yyyy"); if (Directory.Exists(strPath) == false) { Directory.CreateDirectory(strPath); } strPath = strPath + "\\" + dt.Year.ToString() + "-" + dt.Month.ToString() + ".txt"; StreamWriter FileWriter = new StreamWriter(strPath, true); //創建日志文件 FileWriter.WriteLine("[" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "] " + message); FileWriter.Close(); //關閉StreamWriter對象 } catch (Exception ex) { string str = ex.Message.ToString(); } }