using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Web.UI
{
public partial class Weixin : System.Web.UI.Page
{
const string Token = "token"; //你的token
protected string strJson = "";
protected string DefaultOrganise;
Lib.SqlHelper sqlHelper = new Lib.SqlHelper();
string Connectionstring = @"Data Source=192.100.60.122;Initial catalog=appdevtestdb;UID=sa;pwd=ZBJT0049admin;";//替换
protected void Page_Load(object sender, EventArgs e)
{
string postStr = "";
// Valid(); //验证通过后可删除此行代码
if (Request.HttpMethod.ToLower() == "post")
{
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
postStr = Encoding.UTF8.GetString(b);
if (!string.IsNullOrEmpty(postStr))
{
ResponseMsg(postStr);
}
}
}
///
/// 返回信息结果(微信信息返回)
///
///
private void ResponseMsg(string weixinXML)
{
//回复消息的部分:你的代码写在这里
XmlDocument doc = new XmlDocument();
doc.LoadXml(weixinXML);
XmlNodeList list = doc.GetElementsByTagName("xml");
XmlNode xn = list[0];
string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
string content = "";
content = xn.SelectSingleNode("//Content").InnerText;
//string content = "";// doc.GetElementsByTagName("content").Item(0).ToString();
if (content.Equals("Hello2BizUser"))
{
content = "欢迎关注!";
}
else
{
content = "现在是北京时间:" + string.Format("{0:f}", DateTime.Now);
}
string strresponse = "";
strresponse = strresponse + "<![CDATA[" + FromUserName + "]]>";
strresponse = strresponse + "<![CDATA[" + ToUserName + "]]>";
strresponse = strresponse + "" + DateTime.Now.Ticks.ToString() + "";
strresponse = strresponse + "<![CDATA[text]]>";
strresponse = strresponse + "<![CDATA[" + content + "]]>";
strresponse = strresponse + "0";
strresponse = strresponse + "";
WriteLog("postStr:" + content);
Response.Write(strresponse);
}
///
///
/// 写日志(用于跟踪)
///
private void WriteLog(string strMemo)
{
if (!Directory.Exists(Server.MapPath(@"logs\")))
{
Directory.CreateDirectory(Server.MapPath(@"logs\"));
}
string filename = Server.MapPath(@"logs/log.txt");
StreamWriter sr = null;
try
{
if (!File.Exists(filename))
{
sr = File.CreateText(filename);
}
else
{
sr = File.AppendText(filename);
}
sr.WriteLine(strMemo);
}
catch
{
}
finally
{
if (sr != null)
sr.Close();
}
}
///
/// 验证微信签名
///
/// * 将token、timestamp、nonce三个参数进行字典序排序
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
///
private bool CheckSignature()
{
string signature = Request.QueryString["signature"].ToString();
string timestamp = Request.QueryString["timestamp"].ToString();
string nonce = Request.QueryString["nonce"].ToString();
string[] ArrTmp = { Token, timestamp, nonce };
Array.Sort(ArrTmp); //字典排序
string tmpStr = string.Join("", ArrTmp);
tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr.Equals(signature))
{
return true;
}
else
{
return false;
}
}
///
/// 验证
///
private void Valid()
{
string echoStr = Request.QueryString["echoStr"].ToString();
if (CheckSignature())
{
if (!string.IsNullOrEmpty(echoStr))
{
Response.Write(echoStr);
Response.End();
}
}
}
}
}