zoukankan      html  css  js  c++  java
  • 微信 编码要UTF8

    <%@ WebHandler Language="C#" Class="Handler" %>
    
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.IO;
    using System.Text;
    using System.Web.Security;
    using System.Xml;
    
    public class Handler : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext param_context)
        {
    
            string postString = string.Empty;
            if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")
            {
                using (Stream stream = HttpContext.Current.Request.InputStream)
                {
                    Byte[] postBytes = new Byte[stream.Length];
                    stream.Read(postBytes, 0, (Int32)stream.Length);
                    postString = Encoding.Default.GetString(postBytes);
                }
    
                if (!string.IsNullOrEmpty(postString))
                {
                    Handle(postString);
                }
            }
            else
            {
                InterfaceTest();
            }
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        //写入日志
        public void WriteLog(string text)
        {
            StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(".") + "\log.txt", true);
            sw.WriteLine(text);
            sw.Close();//写入
        }
    
        /// <summary>
        /// 处理信息并应答
        /// </summary>
        private void Handle(string postStr)
        {
            WriteLog(postStr);        //记录微信服务器post字串
            messageHelper help = new messageHelper();
            string responseContent = help.ReturnMessage(postStr);
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.Write(responseContent);
            WriteLog(responseContent);   //记录返回微信服务器字串
    
        }
    
        //成为开发者url测试,返回echoStr
        public void InterfaceTest()
        {
            string token = "填写的token";
            if (string.IsNullOrEmpty(token))
            {
                return;
            }
    
            string echoString = HttpContext.Current.Request.QueryString["echoStr"];
            string signature = HttpContext.Current.Request.QueryString["signature"];
            string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
            string nonce = HttpContext.Current.Request.QueryString["nonce"];
    
            if (!string.IsNullOrEmpty(echoString))
            {
                HttpContext.Current.Response.Write(echoString);
                HttpContext.Current.Response.End();
            }
        }
      
    
    }
  • 相关阅读:
    [精华] Linux内存:内存管理的实质
    阿里巴巴笔试题
    LINUX内核经典面试题(转)
    常见网络编程面试题答案征集与面试题(收集)
    转使用jQuery Ajax的内存回收
    模式识别与机器学习
    vc2010 学习笔记2
    vc2010 学习笔记3
    Android高级编程(笔记)第6章 数据存储、检索和共享—3SQLite数据库
    Android高级编程(笔记)第6章 数据存储、检索和共享1共享Preference
  • 原文地址:https://www.cnblogs.com/sekon/p/4691085.html
Copyright © 2011-2022 走看看