1 using System; 2 using System.IO; 3 using System.Text; 4 using System.Web; 5 using System.Web.Security; 6 using System.Xml; 7 8 namespace WebWeiXin 9 { 10 public partial class weixinmsgread : System.Web.UI.Page 11 { 12 13 const string Token = "king"; //与那边填写的token一致 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 string postStr = ""; 17 if (Request.HttpMethod.ToLower() == "post") 18 { 19 Stream s = HttpContext.Current.Request.InputStream; 20 byte[] b = new byte[s.Length]; 21 s.Read(b, 0, (int)s.Length); 22 postStr = Encoding.UTF8.GetString(b); 23 WriteTxt(postStr); 24 25 if (!string.IsNullOrEmpty(postStr)) 26 { 27 try 28 { 29 30 31 //封装请求类 32 XmlDocument doc = new XmlDocument(); 33 doc.LoadXml(postStr); 34 XmlElement rootElement = doc.DocumentElement; 35 36 XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); 37 38 RequestXML requestXML = new RequestXML(); 39 requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText; 40 requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText; 41 requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText; 42 requestXML.MsgType = MsgType.InnerText; 43 44 if (requestXML.MsgType == "text") 45 { 46 requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; 47 } 48 else if (requestXML.MsgType == "location") 49 { 50 requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; 51 requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; 52 requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; 53 requestXML.Label = rootElement.SelectSingleNode("Label").InnerText; 54 } 55 else if (requestXML.MsgType == "image") 56 { 57 requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText; 58 } 59 60 PrintMsg(requestXML); 61 } 62 catch (Exception ex) 63 { 64 WriteTxt(ex + ""); 65 //throw; 66 } 67 } 68 } 69 else 70 { 71 Valid(); 72 } 73 } 74 75 private void PrintMsg(RequestXML requestXML) 76 { 77 string resMsg; 78 string resxml; 79 switch (requestXML.MsgType) 80 { 81 case "text": 82 resMsg = "您输入的是:" + requestXML.Content; 83 break; 84 case "location": 85 resMsg = "您输入的是:" + requestXML.Location_X + "," + requestXML.Location_Y; 86 break; 87 case "image": 88 resMsg = "您输入的是:" + requestXML.PicUrl; 89 break; 90 default: 91 resMsg = "<![CDATA[正在建设中,稍等时日,更多精彩……]]>"; 92 break; 93 } 94 95 resxml = "<xml><ToUserName><![CDATA[" + requestXML.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + 96 requestXML.ToUserName + "]]></FromUserName><CreateTime>" + ConvertDateTimeInt(DateTime.Now) + 97 "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content>" 98 + resMsg 99 + "</Content><FuncFlag>0</FuncFlag></xml>"; 100 WriteTxt(resxml); 101 Response.Write(resxml); 102 } 103 104 /// <summary> 105 /// 验证微信签名 106 /// </summary> 107 /// * 将token、timestamp、nonce三个参数进行字典序排序 108 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 109 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 110 /// <returns></returns> 111 private bool CheckSignature() 112 { 113 string signature = Request.QueryString["signature"]; 114 string timestamp = Request.QueryString["timestamp"]; 115 string nonce = Request.QueryString["nonce"]; 116 string[] ArrTmp = { Token, timestamp, nonce }; 117 Array.Sort(ArrTmp); //字典排序 118 string tmpStr = string.Join("", ArrTmp); 119 tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); 120 tmpStr = tmpStr.ToLower(); 121 122 if (tmpStr == signature) 123 { 124 return true; 125 } 126 else 127 { 128 return false; 129 } 130 } 131 132 private void Valid() 133 { 134 string echoStr = Request.QueryString["echoStr"]; 135 if (CheckSignature()) 136 { 137 if (!string.IsNullOrEmpty(echoStr)) 138 { 139 Response.Write(echoStr); 140 Response.End(); 141 } 142 } 143 } 144 145 146 147 /// <summary> 148 /// unix时间转换为datetime 149 /// </summary> 150 /// <param name="timeStamp"></param> 151 /// <returns></returns> 152 private DateTime UnixTimeToTime(string timeStamp) 153 { 154 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 155 long lTime = long.Parse(timeStamp + "0000000"); 156 TimeSpan toNow = new TimeSpan(lTime); 157 return dtStart.Add(toNow); 158 } 159 160 /// <summary> 161 /// datetime转换为unixtime 162 /// </summary> 163 /// <param name="time"></param> 164 /// <returns></returns> 165 private int ConvertDateTimeInt(System.DateTime time) 166 { 167 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); 168 return (int)(time - startTime).TotalSeconds; 169 } 170 171 /// <summary> 172 /// 记录bug,以便调试 173 /// </summary> 174 /// <returns></returns> 175 public bool WriteTxt(string str) 176 { 177 try 178 { 179 FileStream fs = new FileStream(Server.MapPath("/bugLog.txt"), FileMode.Append); 180 StreamWriter sw = new StreamWriter(fs); 181 //开始写入 182 sw.WriteLine(str); 183 //清空缓冲区 184 sw.Flush(); 185 //关闭流 186 sw.Close(); 187 fs.Close(); 188 } 189 catch (Exception) 190 { 191 return false; 192 } 193 return true; 194 } 195 196 197 198 199 200 201 } 202 203 //微信请求类 204 public class RequestXML 205 { 206 private string toUserName; 207 /// <summary> 208 /// 消息接收方微信号,一般为公众平台账号微信号 209 /// </summary> 210 public string ToUserName 211 { 212 get { return toUserName; } 213 set { toUserName = value; } 214 } 215 216 private string fromUserName; 217 /// <summary> 218 /// 消息发送方微信号 219 /// </summary> 220 public string FromUserName 221 { 222 get { return fromUserName; } 223 set { fromUserName = value; } 224 } 225 226 private string createTime; 227 /// <summary> 228 /// 创建时间 229 /// </summary> 230 public string CreateTime 231 { 232 get { return createTime; } 233 set { createTime = value; } 234 } 235 236 private string msgType; 237 /// <summary> 238 /// 信息类型 地理位置:location,文本消息:text,消息类型:image 239 /// </summary> 240 public string MsgType 241 { 242 get { return msgType; } 243 set { msgType = value; } 244 } 245 246 private string content; 247 /// <summary> 248 /// 信息内容 249 /// </summary> 250 public string Content 251 { 252 get { return content; } 253 set { content = value; } 254 } 255 256 private string location_X; 257 /// <summary> 258 /// 地理位置纬度 259 /// </summary> 260 public string Location_X 261 { 262 get { return location_X; } 263 set { location_X = value; } 264 } 265 266 private string location_Y; 267 /// <summary> 268 /// 地理位置经度 269 /// </summary> 270 public string Location_Y 271 { 272 get { return location_Y; } 273 set { location_Y = value; } 274 } 275 276 private string scale; 277 /// <summary> 278 /// 地图缩放大小 279 /// </summary> 280 public string Scale 281 { 282 get { return scale; } 283 set { scale = value; } 284 } 285 286 private string label; 287 /// <summary> 288 /// 地理位置信息 289 /// </summary> 290 public string Label 291 { 292 get { return label; } 293 set { label = value; } 294 } 295 296 private string picUrl; 297 /// <summary> 298 /// 图片链接,开发者可以用HTTP GET获取 299 /// </summary> 300 public string PicUrl 301 { 302 get { return picUrl; } 303 set { picUrl = value; } 304 } 305 } 306 }
个人小站 欢迎来踩 http://bbsspace.net/