您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

微信开发asp.net

2025/10/18 23:39:12发布17次查看
最近在接触微信开发,也有在看php的代码,不过最后还是使用c#语言了;后台新建了index.ashx文件,这样速度比较快;
首先顶部引用了
using system.io;
using system.xml;
一个是为了实现接收xml文件流,一个是为了后面对xml文件的处理;
public class index : ihttphandler {     private readonly string token = xxxx;//与微信公众账号后台的token设置保持一致,区分大小写。          public void processrequest (httpcontext context) {         context.response.contenttype = text/plain;         string signature = context.request[signature];         string timestamp = context.request[timestamp];         string nonce = context.request[nonce];         string echostr = context.request[echostr];         if (context.request.httpmethod == get)         {             if (checksign(signature, timestamp, nonce))             {                 context.response.output.write(echostr);             }         }         else         {             //post method - 当有用户想公众账号发送消息时触发,写事件         }         context.response.end();     }
首先设置好token,接收各种参数,请求方式是以get的方式发送;
这里主要呢是checksign()函数;
public bool checksign(string signature, string timestamp, string nonce)     {         string[] strs = new string[] { token, timestamp, nonce };         array.sort(strs);//排序         string strnew = string.join(, strs);//连接成字符串         strnew = system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(strnew, sha1);//加密         if (signature == strnew.tolower())             return true;         return false;     }
其实这里的意识就是接收到a/b/c/d,e为自定义,b/c/e生成f,与a比较,相等返回输出d;
string xmlfromwechat = new streamreader(context.request.inputstream).readtoend();//读取xml流             xmldocument xmldocument = new xmldocument();             xmldocument.loadxml(xmlfromwechat);加载字符串             string fromcontent = xmldocument.getelementsbytagname(content).item(0).innertext;             string frommsgtype = xmldocument.getelementsbytagname(msgtype).item(0).innertext;
写的不好指出哈!!
这样我们就可以对接收到的数据进行判断,做出相应的操作,最主要的还是要熟悉接口;
下面就一个例子说明一下,可能没有抽象的很好:
public string receivetext(string xmlfromwechat)     {         xmldocument xmltext = new xmldocument();         xmltext.loadxml(xmlfromwechat);         string content;         string xmlstr;         string keyword = xmltext.getelementsbytagname(content).item(0).innertext.trim();                         content = 欢迎关注xxx!;                string[] defarray = { xmltext.getelementsbytagname(fromusername).item(0).innertext,                                xmltext.getelementsbytagname(tousername).item(0).innertext,                                convertdatetimeint(datetime.now).tostring(),                               content};                xmlstr = transmittext(defarray);                         }                           return xmlstr;     }
public string transmittext(string[] xmlarray)     {         string xmlstring = @<xml>                             <tousername><![cdata[{0}]]></tousername>                             <fromusername><![cdata[{1}]]></fromusername>                             <createtime>{2}</createtime>                             <msgtype><![cdata[text]]></msgtype>                             <content><![cdata[{3}]]></content>                             </xml>;         string xmlstr = string.format(xmlstring, xmlarray);         return xmlstr;     }
这样子就完成的一个简单的回复了;
更多微信开发asp.net。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product