微信公共平台开发 .net
using System;token验证已经通过 不知道为什么没有反应大神们帮帮忙 微信 XML
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;
public partial class weixin_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string postStr = "";
// WriteLog("postStr:" + content);
// Response.Write(strresponse);
if (Request.HttpMethod.ToLower() == "post")
{
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
string apostStr = Encoding.UTF8.GetString(b);
if (!string.IsNullOrEmpty(apostStr))
{
ResponseMsg(apostStr);
}
}
}
/// <summary>
/// 返回信息结果(微信信息返回)
/// </summary>
/// <param name="weixinXML"></param>
private void ResponseMsg(string weixinXML)
{
//回复消息的部分:你的代码写在这里
XmlDocument doc = new XmlDocument();
doc.LoadXml(weixinXML);
XmlNodeList list = doc.GetElementsByTagName("xml");
XmlNode xn = list[0];
string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
string content = "";
content = xn.SelectSingleNode("//Content").InnerText;
//调用翻译方法
content = Query(content);
//组织xml回复
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>";
Response.Write(strresponse);
}
/// <summary>
/// 翻译
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public string Query(string s)
{
string result = "", trans = "", phonetic = "", web = "";
int flag = 0, i = 0;
XmlDocument xd = new XmlDocument();
xd.Load("http://fanyi.youdao.com/openapi.do?keyfrom=microducom&key=1360214796&type=data&doctype=xml&version=1.1&q=" + s);
XmlNodeList fylist = xd.GetElementsByTagName("basic");
foreach (XmlNode xn1 in fylist)
{
XmlNode xex = xn1.SelectSingleNode("//explains");
XmlNodeList exs = xex.ChildNodes;
foreach (XmlNode ex in exs)
{
XmlElement xe2 = (XmlElement)ex;
trans += xe2.InnerText + "\n";
flag = 1;
}
if (xn1.SelectSingleNode("//phonetic") != null)
phonetic = xn1.SelectSingleNode("//phonetic").InnerText;
}
XmlNodeList weblist = xd.SelectSingleNode("//web").ChildNodes;
if (weblist != null)
{
for (i = 0; i < weblist.Count; i++)
{
XmlElement xn2 = (XmlElement)weblist[i];
//web += xn2.Value+"\n";
if (xn2.HasChildNodes)
{
XmlNodeList web2 = xn2.ChildNodes;
foreach (XmlNode xn3 in web2)
{
if (xn3.HasChildNodes)
{
XmlNodeList web3 = xn3.ChildNodes;
foreach (XmlNode xn4 in web3)
{
web += xn4.InnerText + "\n";
}
}
}
}
}
}
if (flag == 1)
{
result = "您要查询的单词:" + s + "\n发音:" + phonetic + "\n" + "释义:" + trans + "\n" + "\n网络释义:\n" + web;
}
else
{
result = "对不起,查询失败!";
}
return result;
}
}
.net
[解决办法]
推荐你用.ashx
[解决办法]
我测试用的代码,已经可用,你拿去参考吧
<%@ WebHandler Language="C#" Class="weixininterface" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.IO;
public class weixininterface : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write(context .Request .QueryString ["echostr"]);
//context.Response.Write("<?xml version="1.0" encoding="utf-8" ?>");
//context.Response.Write("<XML>");
//context.Response.Write(context .Request .Params ["aa"]);
//context.Response.Write("</XML>");
// context.Response.Write("Hello World");
var inputStream = context.Request.InputStream;
var strLen = Convert.ToInt32(inputStream.Length);
var strArr = new byte[strLen];
inputStream.Read(strArr, 0, strLen);
string requestMes = System.Text.Encoding.UTF8.GetString(strArr);
string ToUserName, FromUserName, CreateTime, sendMsg, rtnMsg;
string[] temp;
temp =requestMes .Split (new char[2]{'[',']'});
ToUserName =temp [2];
FromUserName = temp[6];
sendMsg =temp [14];
rtnMsg = "现在是测试,所有内容都是由架设在szd的服务器发出,可以接受的输入有:\n 1 当前时间 \n 2 \n 3";
if (sendMsg == "1")
{
rtnMsg = "现在时间是"+DateTime .Now .ToString();
}
if (sendMsg == "2")
{
rtnMsg = "你输入的是2 二";
}
if (sendMsg == "3")
{
rtnMsg = "你输入的是3 三";
}
string rtn = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName> <CreateTime>1369287835</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA["+rtnMsg+"]]></Content> <FuncFlag>0</FuncFlag> </xml>";
System.IO.FileStream fs = new FileStream("C:\\inetpub\\wwwrootaaa.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(ToUserName);
sw.WriteLine(FromUserName);
sw.WriteLine(sendMsg);
// sw.WriteLine(s2);
sw.Close();
fs.Close();
context.Response.Write(rtn);
}
public bool IsReusable
{
get
{
return false;
}
}
}