当用户关注微信的时候,怎么发一条消息??
本帖最后由 dingzongyinnihao 于 2013-07-01 10:47:16 编辑
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string postStr = "";
if (context.Request.HttpMethod.ToLower() == "post")
{
System.IO.Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
postStr = System.Text.Encoding.UTF8.GetString(b);
if (!string.IsNullOrEmpty(postStr))
{
ResponseMsg(postStr);
}
}
}
public void ResponseMsg(string weixinXML)
{
//回复消息的部分:你的代码写在这里
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(weixinXML);
System.Xml.XmlNodeList list = doc.GetElementsByTagName("xml");
System.Xml.XmlNode xn = list[0];
string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
string content ="";
content = xn.SelectSingleNode("//Content").InnerText;
if (content.Equals("nihao"))
{
content = "你好";
}
else if(content.Equals("hello"))
{
content = "您好,您已经测试成功了";
}
string strresponse = "<xml>";
strresponse = strresponse + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
strresponse = strresponse + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
strresponse = strresponse + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
strresponse = strresponse + "<MsgType><![CDATA[text]]></MsgType>";
strresponse = strresponse + "<Content><![CDATA[" + content + "]]></Content>";
strresponse = strresponse + "<FuncFlag>0</FuncFlag>";
strresponse = strresponse + "</xml>";
HttpContext.Current.Response.Write(strresponse);
}