请教ASP.Net C#微信开发后台程序 30
哪位大神帮我看看我写的代码哪里有问题,我想实现微信关注以后自动回复,可是怎么都实现不了.代码如下://TextMsgXMLpublicstringtextMsg(stri...
哪位大神帮我看看我写的代码哪里有问题, 我想实现微信关注以后自动回复,可是怎么都实现不了.
代码如下:
// Text Msg XML
public string textMsg(string fromUserName, string toUserName, string returnStr)
{
string strXml = "";
strXml = "<xml>"
+ "<ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>"
+ "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>"
+ "<CreateTime>" + Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhh24mmss")) + "</CreateTime>"
+ "<MsgType><![CDATA[text]]></MsgType>"
+ "<Content><![CDATA[" + returnStr + "]]></Content>"
+ "<FuncFlag>0<FuncFlag>"
+ "</xml>";
return strXml;
}
//获取事件
Stream inputstream = Page.Request.InputStream;
byte[] b = new byte[inputstream.Length];
inputstream.Read(b, 0, (int)inputstream.Length);
//string inputstr = System.Text.Encoding.Default.GetString(b);
string inputstr = System.Text.Encoding.UTF8.GetString(b);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); //定义xml实例
doc.LoadXml(inputstr);
string FromUserName = doc.GetElementsByTagName("FromUserName")[0].InnerText;
WXUser wxuser = GetWXUserInfobyOpenID(FromUserName, access_token);
string toUserName = doc.GetElementsByTagName("ToUserName")[0].InnerText;
string msgType = doc.GetElementsByTagName("MsgType")[0].InnerText;
string content = doc.GetElementsByTagName("Content")[0].InnerText;
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
if (msgType == "event")
{
string Event = doc.GetElementsByTagName("Event")[0].InnerText;
if (Event.ToLower() == "subscribe")
{
//想在这里实现关注自动回复, 请大神帮忙看看, 谢谢!!
Response.Write(textMsg(FromUserName,toUserName,
@"微信平台测试数据"));
}
} 展开
代码如下:
// Text Msg XML
public string textMsg(string fromUserName, string toUserName, string returnStr)
{
string strXml = "";
strXml = "<xml>"
+ "<ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>"
+ "<FromUserName><![CDATA[" + toUserName + "]]></FromUserName>"
+ "<CreateTime>" + Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhh24mmss")) + "</CreateTime>"
+ "<MsgType><![CDATA[text]]></MsgType>"
+ "<Content><![CDATA[" + returnStr + "]]></Content>"
+ "<FuncFlag>0<FuncFlag>"
+ "</xml>";
return strXml;
}
//获取事件
Stream inputstream = Page.Request.InputStream;
byte[] b = new byte[inputstream.Length];
inputstream.Read(b, 0, (int)inputstream.Length);
//string inputstr = System.Text.Encoding.Default.GetString(b);
string inputstr = System.Text.Encoding.UTF8.GetString(b);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); //定义xml实例
doc.LoadXml(inputstr);
string FromUserName = doc.GetElementsByTagName("FromUserName")[0].InnerText;
WXUser wxuser = GetWXUserInfobyOpenID(FromUserName, access_token);
string toUserName = doc.GetElementsByTagName("ToUserName")[0].InnerText;
string msgType = doc.GetElementsByTagName("MsgType")[0].InnerText;
string content = doc.GetElementsByTagName("Content")[0].InnerText;
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
if (msgType == "event")
{
string Event = doc.GetElementsByTagName("Event")[0].InnerText;
if (Event.ToLower() == "subscribe")
{
//想在这里实现关注自动回复, 请大神帮忙看看, 谢谢!!
Response.Write(textMsg(FromUserName,toUserName,
@"微信平台测试数据"));
}
} 展开
1个回答
展开全部
代码没什么问题,自己调试一下,这个不能在本地加断点,只能在服务器写日志,跟踪以下几个值:inputsream、msgtype、event,查看它们的值,应该能发现问题。
追问
大神能不能发一段在服务器写日志的代码给我啊...我没做过, 谢谢!!
追答
/// <summary>
/// 记录bug,以便调试
/// </summary>
/// <returns></returns>
protected bool WriteTxt(string str)
{
try
{
FileStream fs = new FileStream(Server.MapPath("/Log.txt"), FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.WriteLine(DateTime.Now.ToString() + ":" + str + "\n");
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
catch (Exception)
{
return false;
}
return true;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询