作者:吃羊的肉 | 来源:互联网 | 2023-05-17 15:54
public void getmsg()
{
Stream stream = HttpContext.Current.Request.InputStream;
Byte[] postBytes = new Byte[stream.Length];
stream.Read(postBytes, 0, (Int32)stream.Length);
string postString = Encoding.UTF8.GetString(postBytes);
if (!string.IsNullOrEmpty(postString))
{
//封装请求类
XmlDocument requestDocXml = new XmlDocument();
requestDocXml.LoadXml(postString);
XmlElement rootElement = requestDocXml.DocumentElement;
textMsg WxXmlModel = new textMsg();
WxXmlModel.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;
WxXmlModel.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;
WxXmlModel.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;
WxXmlModel.MsgType = rootElement.SelectSingleNode("MsgType").InnerText;
switch (WxXmlModel.MsgType)
{
case "text"://文本
WxXmlModel.Content = rootElement.SelectSingleNode("Content").InnerText;
break;
default:
break;
}
requestDocXml.Save("D:/get.xml");
backmsg(WxXmlModel);//回复消息
}
}
public void backmsg(textMsg WxXmlModel)
{
string textpl = string.Empty;
textpl = "" +
"" +
"" +
"" + ConvertDateTimeInt(DateTime.Now) + "" +
"" +
"" +
"0" +
"";
XmlDocument xml = new XmlDocument();
xml.LoadXml(textpl);
if (!string.IsNullOrEmpty(textpl))
{
HttpContext.Current.Response.Write(xml);
HttpContext.Current.Response.End();
}
}
public int ConvertDateTimeInt(DateTime dt)
{
dt = DateTime.Now;
string strdt = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString();
strdt = strdt.Substring(2);
int intdt = Convert.ToInt32(strdt);
return intdt;
}
找来找去找不到问题在哪,我直接手动把xml格式的写入读取xml变量,然后保存要发送的xml,出来倒是正常的。请各位指教下问题在哪,不胜感激
9 个解决方案
自己拼接微信的XML参数,这种是容易出错的.一般做法是,找到这个接口的文档,将它的示例XML代码直接复制过来.然后直接改里面的值.如果需要动态生成,也是将这示例做为模板.
谢谢楼上两位的建议,但是我中间有一步是把xml保存到本地,我向微信发现信息后,本地什么都没有,所以我觉得是不是接收的地方有问题?