第一句話都會這麼去寫:程式猿就是苦逼,除了開發還要會寫博文!今天咱就和大家探讨下如何讓自己成為開發者!那麼怎麼才能成為開發者呢?
首先給大家一個微信的截圖,看到這個截圖,是不是有想去嘗試的沖動?
C#開發微信,一般都要寫一個一般處理程式,就像截圖中的.../CommonCS/WeiXin.ashx,這個一般處理程式會接收使用者消息或開發者進行的事件推送。例如:使用者關注時,微信伺服器會将關注的資料包轉發到咱們寫的一般處理程式上。再例如:取消關注,使用者發送文本消息,開發者群發消息等等吧!好多呢,在此不一一舉例了。
現在進入正題,直接貼代碼,然後再作講解:
#region 程式入口
/// <summary>
/// 程式入口
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
try
{
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "GET")
{
Auth(); //微信接入的測試 成為開發者第一步
}
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
}
catch(Exception ex)
LogHelper.WriteLog("系統故障。", ex);
}
public bool IsReusable
get
return false;
#endregion
LogHelper.WriteLog("系統故障。", ex);在此,我使用的是Log4Net,用于記錄系統出現的錯誤。當然你也可以自己寫一個日志類,不過沒有Log4Net強大罷了。在此,我提供一些日志方法,僅供參考。如下:
#region 日志類 記錄異常和消息 注:在本系統中,請勿使用。請使用Log4Net
#region 字段
public static object _lock = new object();
#region 寫檔案
/// 寫檔案
public static void WriteFile(string log, string path)
Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)
lock (_lock)
if (!File.Exists(path))
{
using (FileStream fs = new FileStream(path, FileMode.Create)) { }
}
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
#region 日志内容
string value = string.Format(@"{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), obj.ToString());
#endregion
sw.WriteLine(value);
sw.Flush();
}
}));
thread.Start(log);
#region 寫日志
/// 寫日志
public static void WriteLog(HttpContext context, string log)
string logPath = context.Request.MapPath(@"\CommonCS\WX_Log.txt");
WriteFile(log, logPath);
#region 寫錯誤日志
/// 寫錯誤日志
public static void WriteErrorLog(HttpContext context, string log)
string logPath = context.Request.MapPath(@"\CommonCS\WX_ErrorLog.txt");
#region 成為開發者
/// 驗證微信簽名
public bool CheckSignature(string token, string signature, string timestamp, string nonce)
string[] ArrTmp = { token, timestamp, nonce };
Array.Sort(ArrTmp);
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature)
return true;
else
/// 成為開發者的第一步,驗證并相應伺服器的資料
private void Auth()
string token = GetXMLstr("URLToken");//自己填寫的Token 和開啟開發者模式時填寫的一樣。
if (string.IsNullOrEmpty(token))
//LogTextHelper.Error(string.Format("WeixinToken 配置項沒有配置!"));
string echoString = HttpContext.Current.Request.QueryString["echostr"];
string signature = HttpContext.Current.Request.QueryString["signature"];
string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
string nonce = HttpContext.Current.Request.QueryString["nonce"];
if (CheckSignature(token, signature, timestamp, nonce))
if (!string.IsNullOrEmpty(echoString))
HttpContext.Current.Response.Write(echoString);
// HttpContext.Current.Response.End();
将上述代碼複制粘貼到你的一般處理程式中。
上述紅色部分,和你啟用開發者模式時填寫的一樣。不過,我将這些資訊放在了XML檔案中,你也可以直接指派給它,例如:
string token="shengshiguanqian";//截圖如下:你懂得。
這些代碼網上很多,自己寫好這個處理程式後,釋出到伺服器,然後按照要求進行微信開發的伺服器配置,路徑要寫對,Token要和開發中的Token一樣。也就是本部落格中的紅色字型。你懂得。
後續,我還會陸陸續續的寫一些關于微信的部落格。在此,感謝做C#的大神公開的代碼,今天我學會了,也公開啦!無私奉獻是一個程式員最美的美德。嘻嘻。
哈哈,嘻嘻,我的蔻蔻:1429677330.有需要源碼的可以加一下。謝謝。