위챗 개발 asp.net

高洛峰
풀어 주다: 2017-02-23 13:51:54
원래의
1511명이 탐색했습니다.

최근 위챗 개발을 접하게 되었고, PHP 코드도 살펴봤지만 결국에는 여전히 C# 언어를 사용했습니다.

백그라운드에서 새로운 index.ashx 파일을 생성했고,

우선 가장 중요한 인용문은

system.IO 사용
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();
    }
로그인 후 복사

먼저 토큰을 설정하고 다양한 매개변수를 수신합니다.

여기서 가장 중요한 것은 CheckSign()입니다. function;

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></tousername>
                            <fromusername></fromusername>
                            <createtime>{2}</createtime>
                            <msgtype></msgtype>
                            <content></content>
                            </xml>";
        string xmlstr = string.Format(xmlstring, xmlArray);
        return xmlstr;
    }
로그인 후 복사
간단한 답변입니다.

위챗 개발 asp.net

WeChat과 관련된 추가 기사 asp.net 개발, PHP 중국어 웹사이트를 주목해주세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!