注册微信微信注册发送短信信zm

74被浏览136400分享邀请回答11 条评论分享收藏感谢收起前两天做个项目用到了微信api功能。项目完成后经过整理封装如下微信操作类。
以下功能的实现需要开发者已有微信的公众平台账号,并且开发模式已开启、接口配置信息已设置完毕。废话不多说直接上代码。
1、公众平台账号接口配置信息中URL接口代码如下:
1 &%@ WebHandler Language="C#" Class="WeixinInterface" %&
4 using System.W
6 using WeiX
7 using System.X
8 public class WeixinInterface : IHttpHandler
public void ProcessRequest(HttpContext context)
this.context =
//WriteLog("[Begin:" + DateTime.Now.ToString() + "]");
//开始接收信息(输出日志,供调试使用)
* 第四个参数"checkToken"为true时,此实例方法用于验证微信公众平台配置的URL、Token。
* 第四个参数"checkToken"为false时,此实例方法用于接收用户信息、回复用户信息。
* 第四个参数"checkToken"为true时,此实例代码最好不要包含于try catch语句中
* 第三个参数从配置文件中读取,其值为微信公众平台接口配置信息中的&Token&
//第一步:实例化微信封装类
WeiXinMessage weixin = new WeiXinMessage(context.Request, context.Response, System.Configuration.ConfigurationManager.AppSettings["Token"].ToString(), false);
//WriteLog("[ReceiveStr:" + reply.ReceiveStr + "]");
//输出接收字符串(输出日志,供调试使用)
//第二步:获取用户发送消息类型
string msgType = weixin.GetMsgType();
//第三步:根据接收到不同的消息类型,执行不同的业务逻辑
if (msgType == "text")
string msg = weixin.GetMsgText();//获取用户发送文本信息
//WriteLog("[UserMsg:" + msg + "]");
//输出用户发送的文本信息(输出日志,供调试使用)
string answer = "";
//根据用户发送的信息,自动匹配自定义的&自动回复&
//answer = HEBCCC.SSSS.BLL.W_ZDHFBLL.GetAnswer(answer);
catch (Exception ex)
WriteLog(DateTime.Now.ToString() + "[error:" + ex.Message + "]");
//WriteLog("[answer:" + answer + "]");
if (!string.IsNullOrEmpty(answer))
//匹配出自动回复内容,推送给用户文本信息
weixin.SendMsgText(answer);//此代码不能包含于try catch语句中,否则报错。
else//匹配不出自动回复内容时,从系统xml配置文件中读取默认文本内容推送给用户
XmlNode autoReplyXmlNode = null;
string path = context.Server.MapPath("~/xml/sys.xml");
XmlDocument xmldom = new XmlDocument();
xmldom.Load(path);//加载xml文件
XmlNode xmlNode = xmldom.SelectSingleNode("root");//读取第一个节点
autoReplyXmlNode = xmlNode.SelectSingleNode("autoReply");//读取第一个节点
catch (Exception ex)
WriteLog(DateTime.Now.ToString() + "[error2:" + ex.Message + "]");
if (autoReplyXmlNode != null && !string.IsNullOrEmpty(autoReplyXmlNode.InnerText))
weixin.SendMsgText(autoReplyXmlNode.InnerText);//此代码不能包含于try catch语句中,否则报错。
else if (msgType == "event")
//获取事件类型(Event)、事件Key值(EventKey)
string[] array = weixin.GetEventType();
if (array[0] == "click" && array[1].ToLower() == "c_khgh")
weixin.SendMsgText("抱歉,此功能暂未开通【河北华网计算机技术有限公司】");
/// &summary&
/// 记录日志
/// &/summary&
/// &param name="strMemo"&内容&/param&
private void WriteLog(string strMemo)
string logPath=System.Configuration.ConfigurationManager.AppSettings["SysLog"].ToString();
string filename = context.Server.MapPath(logPath + "\\WeiXin.txt");
if (!System.IO.Directory.Exists(context.Server.MapPath(logPath)))
System.IO.Directory.CreateDirectory(context.Server.MapPath(logPath));
System.IO.StreamWriter sr =null;
if (!System.IO.File.Exists(filename))
System.IO.File.CreateText(filename);
sr = System.IO.File.AppendText(filename);
sr.WriteLine(strMemo);
if (sr != null)
sr.Close();
public bool IsReusable
return false;
注:以上接口代码实现了,接收用户短信、被动向用户推送短信(自动回复)功能。由于我自己项目本身功能的要求,我只用到的接收用户文本短信、接收用户推送事件、向用户推送文本信息的功能。所以在我封装的类库里也只有这些功能。其他功能:如接收图片消息、接收地理位置消息、接收链接消息、推送音乐消息、推送图文消息并未实现。
2、自定义菜单
接口完成后,下面就该配制自己的自定义菜单了。我这里做了一个测试页,用于配制自定义菜单。
现实现三个功能:获取凭证(access_token)、创建菜单(需access_token)、删除菜单(需access_token)
获取凭证所需的appid、appsecret在微信公众平台的接口配置信息里。
代码如下:
1 &%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&
3 &!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&
4 &html xmlns="http://www.w3.org/1999/xhtml"&
5 &head runat="server"&
&title&&/title&
&form id="form1" runat="server"&
&td align="right"&
&asp:TextBox ID="_txtAppid" runat="server"&&/asp:TextBox&
&td align="right"&
appsecret:
&asp:TextBox ID="_txtAppsecret" runat="server"&&/asp:TextBox&
&td colspan="2" align="center"&
&asp:Button ID="_btnAccessToken" runat="server" Text="获取access_token" OnClick="_btnAccessToken_Click" /&
&asp:Label ID="_lblMsg" runat="server" Text=""&&/asp:Label&
&td align="right"&access_token:&/td&
&asp:TextBox ID="_txtacetoken" TextMode="MultiLine" Rows="5" Columns="40" runat="server"&&/asp:TextBox&
&td align="right"&自定义菜单内容:&/td&
&asp:TextBox ID="_txtMenu" TextMode="MultiLine" Rows="10" Columns="40" runat="server"&&/asp:TextBox&
&td colspan="2" align="center"&
&asp:Button ID="_btnSetMenu" runat="server" Text="设置菜单"
onclick="_btnSetMenu_Click"
&asp:Button ID="_btnDelMenu" runat="server" Text="删除自定义菜单"
onclick="_btnDelMenu_Click"
&asp:Label ID="_lblMsg2" runat="server" Text=""&&/asp:Label&
66 &/body&
67 &/html&
2 using System.Collections.G
3 using System.L
4 using System.W
5 using System.Web.UI;
6 using System.Web.UI.WebC
8 using System.N
9 using System.IO;
10 using System.T
11 public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
//获取access_token
protected void _btnAccessToken_Click(object sender, EventArgs e)
if (string.IsNullOrEmpty(_txtAppid.Text.Trim()))
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('请输入appid');", true);
if (string.IsNullOrEmpty(_txtAppsecret.Text.Trim()))
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('请输入appsecret');", true);
string url = "https://api./cgi-bin/token?grant_type=client_credential&appid=" + _txtAppid.Text.Trim() + "&secret=" + _txtAppsecret.Text.Trim();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "get";
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
_lblMsg.Text = "返回结果:" + sr.ReadToEnd();
//设置菜单
protected void _btnSetMenu_Click(object sender, EventArgs e)
if (string.IsNullOrEmpty(_txtacetoken.Text.Trim()))
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('请输入access_token');", true);
if (string.IsNullOrEmpty(_txtMenu.Text.Trim()))
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('请输入自定义菜单内容');", true);
string padata = _txtMenu.Text.Trim();
string url = "https://api./cgi-bin/menu/create?access_token=" + _txtacetoken.Text.Trim();//请求的URL
byte[] byteArray = Encoding.UTF8.GetBytes(padata); // 转化
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentLength = byteArray.L
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
_lblMsg2.Text = "返回结果:" + sr.ReadToEnd();
catch (Exception ex)
//删除菜单
protected void _btnDelMenu_Click(object sender, EventArgs e)
if (string.IsNullOrEmpty(_txtacetoken.Text.Trim()))
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('请输入access_token');", true);
string url = "https://api./cgi-bin/menu/delete?access_token=" + _txtacetoken.Text.Trim();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "get";
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
_lblMsg2.Text = "返回结果:" + sr.ReadToEnd();
说明:这里附上单文件dll。整个项目的源码已放入到CSDN中,需要的朋友可以去下,在这就不单独上传了。&
  CSDN连接:http://download.csdn.net/detail/xujie825/6329293
  还有一点就是微信的主动推送,虽然微信对外没有公开主动推送的api接口,但是经过我们的努力完全是可以实现的。我的项目里已经过验证。由于微信不提倡,所以这里也就不附上主动推送的代码了。有学习精神的朋友们,多努力努力吧。
阅读(...) 评论()微信公众平台开发实战:手机短信验证功能
视频太卡?试试切换线路
本课时介绍短信验证码、短信验证码接口及 PHP 对 MySQL 数据库操作的一些基本知识,为后面实战课程奠定基础。
本课时讲解短信验证码接口的实例调用,并处理得到有效数据。
本课时学习通过 PHP 对 MySQL 数据库的操作,将发送的短信验证码相关信息存入数据库。
本课时学习 PHP 对 MySQL 查询的操作、PHP 接收提交数据及通过程序判断,实现手机短信验证功能。
只有成为VIP会员才能提问&回复,快吧!如果你还没有账号你可以一个账号。
添加新技术问题
课程 [短信验证码及 PHP 对 MySQL 操作的介绍]
中已存在问题
添加新技术问题
问题描述越详细,被解答的速度越快
有新回答时请邮件提醒我
着急,拜托快点
不急,慢慢解决
关联课程 [短信验证码及 PHP 对 MySQL 操作的介绍]}

我要回帖

更多关于 微信注册 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信