关于微信接收用户文本消息和被动自动响应回复
<%@ WebHandler Language="C#" Class="wxinte易做图ce" %>using System;
using System.Web;
using System.Data;
using System.IO;
public class wxinte易做图ce : IHttpHandler
{
HttpContext context = null;
//测试(记录日志到log文件)
private void w(string str)
{
System.IO.File.AppendAllText(@"C:\log.log", "\r\nTime:" + DateTime.Now.ToString() + "\t" + str);
}
public void ProcessRequest(HttpContext contextss)
{
context = contextss;
w("BeginRequest");
if (!string.IsNullOrEmpty(context.Request.Params["echostr"]))
{
valid();
}
else
{
if (context.Request.HttpMethod.ToLower() == "post")
{
context.Response.ContentType = "text/xml";
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);
w("RequestData:" + requestMes);
string ToUserName, FromUserName, sendMsg, rtnMsg;
string[] temp;
temp = requestMes.Split(new char[2] { '[', ']' });
//temp[2] url,temp[6] TouserName,temp[10] fromusername,temp[14] type,temp[18] content
ToUserName = temp[6];
FromUserName = temp[10];
sendMsg = temp[18] + "";
rtnMsg = "现在是测试,可以输入有:\n 1 当前时间 \n 2 \n 3";
if (sendMsg == "1")
{
rtnMsg = "现在时间是" + DateTime.Now.ToString();
}
if (sendMsg == "2")
{
rtnMsg = "你输入的是二";
}
if (sendMsg == "3")
{
rtnMsg = "你输入的是三";
}
else
{
rtnMsg = "没这个选项哈";
}
//组织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[" + rtnMsg + "]]></Content>";
strresponse = strresponse + "</xml>";
w("ResponseData:" + strresponse);
context.Response.Write(strresponse);
context.Response.End();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
//验证
public void valid()
{
var echostr = context.Request.QueryString["echoStr"];
if (checkSignature() && !string.IsNullOrEmpty(echostr))
{
context.Response.Write(echostr);
context.Response.End();//推送验证token
}
}
//检测
public bool checkSignature()
{
var signature = context.Request.QueryString["signature"];
var timestamp = context.Request.QueryString["timestamp"];
var nonce = context.Request.QueryString["nonce"];
var token = "HesToKen";
string[] ArrTmp = { token, timestamp, nonce };
Array.Sort(ArrTmp);
string tmpStr = string.Join("", ArrTmp);
tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
tmpStr = tmpStr.ToLower();
if (tmpStr == signature)
{
return true;
}
else
{
return false;
}
}
}
//以下是我在微信接口调试返回的结果
返回结果: 200 OK
Content-Length: 225
Content-Type: text/xml; charset=utf-8
Cache-Control: private
X-Powered-By: ASP.NETX-AspNet-Version: 4.0.30319
Server: Microsoft-IIS/6.0
Date: Fri, 29 Nov 2013 03:20:19 GMT
Connection: keep-alive
<xml>
<ToUserName>
<![CDATA[test]]>
</ToUserName>
<FromUserName>
<![CDATA[hptst]]>
</FromUserName>
<CreateTime>635213208195373750</CreateTime>
<MsgType>
<![CDATA[text]]>
</MsgType>
<Content>
<![CDATA[没这个选项哈]]>
</Content>
</xml>
提示: 请求成功
//问题是:为什么我在手机上发微信却没有任何反应呢?请求高手帮助解答 微信开发 微信用户消息接收及自动响应回复消息 --------------------编程问答-------------------- 求大神帮忙啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊阿 --------------------编程问答-------------------- 首先,DateTime.Now.Ticks.ToString() 是错的
其次,你到底要响应哪个事件 的消息都没指明
不知道你是怎么接口调试的,我是不知道怎么用那个在线接口调试,那里面很多东西乱填都可以。
补充:.NET技术 , C#