微信公众号代码怎么写?微信公共号代码分享

作者:鱼爪创媒网 点击:43 发布时间:2022-03-10

  微信公众号代码怎么写?微信公共号代码分享。微信公众号代码应该怎么写呢?许多微信朋友都为此表示心有余而力不足,其实很简单,下面小编和大家分享一些微信公众号的相关代码,供大家参考,也希望能帮助到大家。

  代码如下:

  using System;

  using System.IO;

  using System.Text;

  using System.Web;

  using System.Web.Security;

  using System.Xml;

  namespace WebWeiXin

  {

  public partial class weixinmsgread : System.Web.UI.Page

  {

  const string Token = "king"; //与那边填写的token一致

  protected void Page_Load(object sender, EventArgs e)

  {

  string postStr = "";

  if (Request.HttpMethod.ToLower() == "post")

  {

  Stream s = HttpContext.Current.Request.InputStream;

  byte[] b = new byte[s.Length];

  s.Read(b, 0, (int)s.Length);

  postStr = Encoding.UTF8.GetString(b);

  WriteTxt(postStr);

  if (!string.IsNullOrEmpty(postStr))

  {

  try

  {

  //封装请求类

  XmlDocument doc = new XmlDocument();

  doc.LoadXml(postStr);

  XmlElement rootElement = doc.DocumentElement;

  XmlNode MsgType = rootElement.SelectSingleNode("MsgType");

  RequestXML requestXML = new RequestXML();

  requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;

  requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;

  requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;

  requestXML.MsgType = MsgType.InnerText;

  if (requestXML.MsgType == "text")

  {

  requestXML.Content = rootElement.SelectSingleNode("Content").InnerText;

  }

  else if (requestXML.MsgType == "location")

  {

  requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText;

  requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText;

  requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText;

  requestXML.Label = rootElement.SelectSingleNode("Label").InnerText;

  }

  else if (requestXML.MsgType == "image")

  {

  requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText;

  }

  PrintMsg(requestXML);

  }

  catch (Exception ex)

  {

  WriteTxt(ex + "");

  //throw;

  }

  }

  }

  else

  {

  Valid();

  }

  }

  private void PrintMsg(RequestXML requestXML)

  {

  string resMsg;

  string resxml;

  switch (requestXML.MsgType)

  {

  case "text":

  resMsg = "您输入的是:" + requestXML.Content;

  break;

  case "location":

  resMsg = "您输入的是:" + requestXML.Location_X + "," + requestXML.Location_Y;

  break;

  case "image":

  resMsg = "您输入的是:" + requestXML.PicUrl;

  break;

  default:

  resMsg = "";

  break;

  }

  resxml = "";

  WriteTxt(resxml);

  Response.Write(resxml);

  }

  ///

  /// 验证微信签名

  ///

  /// * 将token、timestamp、nonce三个参数进行字典序排序

  /// * 将三个参数字符串拼接成一个字符串进行sha1加密

  /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。

  ///

  private bool CheckSignature()

  {

  string signature = Request.QueryString["signature"];

  string timestamp = Request.QueryString["timestamp"];

  string nonce = Request.QueryString["nonce"];

  string[] ArrTmp = { Token, timestamp, nonce };

  Array.Sort(ArrTmp); //字典排序

  string tmpStr = string.Join("", ArrTmp);

  tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");

  tmpStr = tmpStr.ToLower();

  if (tmpStr == signature)

  {

  return true;

  }

  else

  {

  return false;

  }

  }

  private void Valid()

  {

  string echoStr = Request.QueryString["echoStr"];

  if (CheckSignature())

  {

  if (!string.IsNullOrEmpty(echoStr))

  {

  Response.Write(echoStr);

  Response.End();

  }

  }

  }

  ///

  /// unix时间转换为datetime

  ///

  ///

  ///

  private DateTime UnixTimeToTime(string timeStamp)

  {

  DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

  long lTime = long.Parse(timeStamp + "0000000");

  TimeSpan toNow = new TimeSpan(lTime);

  return dtStart.Add(toNow);

  }

  ///

  /// datetime转换为unixtime

  ///

  ///

  ///

  private int ConvertDateTimeInt(System.DateTime time)

  {

  System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));

  return (int)(time - startTime).TotalSeconds;

  }

  ///

  /// 记录bug,以便调试

  ///

  ///

  public bool WriteTxt(string str)

  {

  try

  {

  FileStream fs = new FileStream(Server.MapPath("/bugLog.txt"), FileMode.Append);

  StreamWriter sw = new StreamWriter(fs);

  //开始写入

  sw.WriteLine(str);

  //清空缓冲区

  sw.Flush();

  //关闭流

  sw.Close();

  fs.Close();

  }

  catch (Exception)

  {

  return false;

  }

  return true;

  }

  }

  //微信请求类

  public class RequestXML

  {

  private string toUserName;

  ///

  /// 消息接收方微信号,一般为公众平台账号微信号

  ///

  public string ToUserName

  {

  get { return toUserName; }

  set { toUserName = value; }

  }

  private string fromUserName;

  ///

  /// 消息发送方微信号

  ///

  public string FromUserName

  {

  get { return fromUserName; }

  set { fromUserName = value; }

  }

  private string createTime;

  ///

  /// 创建时间

  ///

  public string CreateTime

  {

  get { return createTime; }

  set { createTime = value; }

  }

  private string msgType;

  ///

  /// 信息类型 地理位置:location,文本消息:text,消息类型:image

  ///

  public string MsgType

  {

  get { return msgType; }

  set { msgType = value; }

  }

  private string content;

  ///

  /// 信息内容

  ///

  public string Content

  {

  get { return content; }

  set { content = value; }

  }

  private string location_X;

  ///

  /// 地理位置纬度

  ///

  public string Location_X

  {

  get { return location_X; }

  set { location_X = value; }

  }

  private string location_Y;

  ///

  /// 地理位置经度

  ///

  public string Location_Y

  {

  get { return location_Y; }

  set { location_Y = value; }

  }

  private string scale;

  ///

  /// 地图缩放大小

  ///

  public string Scale

  {

  get { return scale; }

  set { scale = value; }

  }

  private string label;

  ///

  /// 地理位置信息

  ///

  public string Label

  {

  get { return label; }

  set { label = value; }

  }

  private string picUrl;

  ///

  /// 图片链接,开发者可以用HTTP GET获取

  ///

  public string PicUrl

  {

  get { return picUrl; }

  set { picUrl = value; }

  }

  }

  }

以上就是小编整理的关于微信公众号代码怎么写和微信公共号代码分享的全部内容。如果您还想了解更多精彩内容,就赶紧来鱼爪创媒吧。本网有更多惊喜内容等着您哦。

  

鱼爪创媒是一家专业提供公众号交易、公众号迁移、公众号增粉、公众号买卖交易的平台,如果您有这方面的需求,欢迎电话咨询:13018202357。有任何疑问,可以 【立即咨询】 我们平台的客服或者添加微信号 【13018202357】 。此文章来源于网络,如有侵权,请联系删除

标签:

资金保障

提供买卖双方资金担保交易
更放心

法律保障

交易签订具有法律效益合同
提供法律支持

急速退款

专属资金通道,快速拿到
钱款

全资公司

全资控股公司,大平台交易
有保障

服务优质

3500+专业团队,为您提供24
小时一对一贴心服务