一贯奉行拿来主义 以下代码直接copy 可用
本代码 使用了网上其他人的代码 !
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Xml;
public class JsonHelper
{
/// <summary>
/// 生成Json格式
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string GetJson<T>(T obj)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, obj);
string szJson = Encoding.UTF8.GetString(stream.ToArray()); return szJson;
}
}
public static string GetJsonObject(object dy)
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(dy);
}
/// <summary>
/// 获取Json的Model
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="szJson"></param>
/// <returns></returns>
public static T ParseFromJson<T>(string szJson)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
/// <summary>
/// 获取Json的Model
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="szJson"></param>
/// <returns></returns>
public static object ObjectFromJson(string szJson)
{
JavaScriptSerializer javado = new JavaScriptSerializer();
return javado.DeserializeObject(szJson);
}
}
public class WXHelp
{
public System.Web.HttpRequest Request;
public System.Web.HttpResponse Response;
string appid = "xxxxxxx";
string appsecret = "xxxxxxxx";
bool CheckSignature()
{
string signature = Request.QueryString["signature"];
string timestamp = Request.QueryString["timestamp"];
string nonce = Request.QueryString["nonce"];
//dsds
string toke = "xxxx";//token
string[] Arrtmp = { toke, timestamp, nonce };
Array.Sort(Arrtmp);
string s = string.Join("", Arrtmp);
s = FormsAuthentication.HashPasswordForStoringInConfigFile(s, "SHA1");
s = s.ToLower();
if (s == signature)
{
return true;
}
return false;
}
// from[o61OnuAy6J7GaEDvDF9onOG5BiLk]to[gh_51009818945f]
public void OnLoad()
{
if (string.IsNullOrEmpty(Request.QueryString["echostr"]))
{
wxmessage wx = GetWxMessage();
string temp = "";
if (wx.EventName == "subscribe")
{
temp = sendTextMessage(wx, @"感谢您关注xxxxxxxxxxxxxxxxxxxx。" );
}
else if (wx.Content == "1")
{
temp = sendTextMessage(wx