C# .ashx background obtains the card swiping information submitted by GET, POST, and JSON, and responds to the driver card reader to display text and broadcast voice

This example uses equipment: RFID network WIFI wireless TCP/UDP/HTTP programmable secondary development card reader POE powered voice-Taobao (taobao.com)

<%@ WebHandler Language="C#" Class="HttpReader" %>

using System;
using System.Web;
using System.IO;
using Newtonsoft.Json;

public class HttpReader : IHttpHandler
{
    public void ProcessRequest (HttpContext context) {
        string info = "";
        string jihao = "";
        string cardtype = "";
        string card = "";
        string Data = "";
        string dn = "";
        string Status = "";
        Int16 cardtype16 = 0;
        int cardtypecode = 0;
        int pushortake = 0;
        string dispstr = "";
        string ChineseVoice = "[v8]"; //[v8] indicates the voice volume of this broadcast, the value range is v1 to v16

        context.Response.ContentType = "text/plain";
        
        if (context.Request["info"] != null) { info = context.Request["info"].ToString(); } //Received data packet number, need to respond to the packet number
        if (context.Request["jihao"] != null) { jihao = context.Request["jihao"].ToString(); } //Equipment number (can be edited by yourself)
        if (context.Request["cardtype"] != null) { cardtype = context.Request["cardtype"].ToString(); } //Card type, card status
        if (context.Request["card"] != null) { card = context.Request["card"].ToString(); } //The original hexadecimal card number received can be used as needed Convert to other card number yourself
        if (context.Request["Data"] != null) { Data = context.Request["Data"].ToString(); } //Sector content
        if (context.Request["dn"] != null) { dn = context.Request["dn"].ToString(); } //Equipment hardware serial number, which has been solidified at the factory and is unique in the world
        if (context.Request["Status"] != null) { Status = context.Request["Status"].ToString(); } //Read card status, such as 12 means card password authentication failed
        
        if (info != "" & amp; & amp; jihao != "" & amp; & amp; cardtype != "" & amp; & amp; card != ""){
                cardtype16 = Convert.ToInt16(cardtype, 16);
                pushortake = cardtype16 / 128; //pushortake=0 means reading the card, >0 means the card leaves the sensing area
                cardtypecode = cardtype16 % 16; //cardtypecode=1 ID card, 2 HID card, 3 T5557 card, 4 EM4305 card, 5 IC card, 6 second generation ID card, 7 is 15693 card, IClass"
         }
         else{ //If no valid parameters are obtained, use JSON method to parse and obtain the submitted parameters.
                StreamReader sr = new StreamReader(context.Request.GetBufferlessInputStream());
                string response = sr.ReadToEnd();
            
                RootObject rb = JsonConvert.DeserializeObject<RootObject>(response);
                info = rb.info; //Received data packet number, need to respond to the packet number
                jihao = rb.jihao; //Equipment number (can be edited by yourself)
                cardtype = rb.cardtype; //card type, card status
                card = rb.card; //The original hexadecimal card number received can be converted into other card numbers as needed.
                Data = rb.data; //Sector content
                dn = rb.dn; //Equipment hardware serial number, solidified at the factory, unique in the world
                Status = rb.status; //Card reading status, such as 12 means card password authentication failed

                if (info != "" & amp; & amp; jihao != "" & amp; & amp; cardtype != "" & amp; & amp; card != "")
                {
                    cardtype16 = Convert.ToInt16(cardtype, 16);
                    pushortake = cardtype16 / 128; //pushortake=0 means reading the card, >0 means the card leaves the sensing area
                    cardtypecode = cardtype16 % 16; // cardtypecode=1 ID card, 2 HID card, 3 T5557 card, 4 EM4305 card, 5 IC card, 6 second-generation ID card, 7 is 15693 card, IClass"
                }
        }

        if (info != "" & amp; & amp; card != "") //The valid parameters are obtained through analysis, and the driver card reader responds by displaying text, beeping sound or broadcasting voice
        {
            dispstr = "{" + getChinesecode("card number") + ":}" + (card + " ").Substring(0, 12) + DateTime.Now.ToString("yy- MM-dd HH:mm:ss"); //Display information. Note that Chinese characters must be converted into codes that the device can display. Other alphanumeric symbols do not need to be converted. Information within {} is displayed in reverse video.

            if (pushortake > 0)
            {
                ChineseVoice = ChineseVoice + getChinesecode("card number") + "[n1]" + card + getChinesecode("Leave the sensing area!"); //TTS voice, please note that Chinese characters must be converted into something that the device can recognize encoding, [n1] represents the digital broadcasting method, other alphanumeric symbols do not need to be converted
            }
            else
            {
                ChineseVoice = ChineseVoice + getChinesecode("Read card number") + "[n1]" + card;
            }

            string RepStr = "Response=1"; //Response=1 fixed prefix, our device uses this to retrieve return information, indicating the drive device display and sound
            RepStr = RepStr + "," + info; //The submitted information serial number must correspond
            RepStr = RepStr + "," + dispstr; //Display text on the card reader
            RepStr = RepStr + ",20"; //Display duration 20 seconds
            RepStr = RepStr + ",2"; //Buzzer sound type, value range 0-12
            RepStr = RepStr + "," + ChineseVoice; //TTS voice broadcast

            context.Response.Write(RepStr);
        }
    }

    public class RootObject //json class
    {
        public string info { get; set; }
        public string jihao { get; set; }
        public string cardtype { get; set; }
        public string card { get; set; }
        public string data { get; set; }
        public string dn { get; set; }
        public string status { get; set; }

    }

    public static string getChinesecode(string inputstr) //Get Chinese code, display Chinese characters and TTS Chinese voice must be converted to code
    {
        int strlen = inputstr.Length;
        string hexcode = "";
        for (int i = 0; i < strlen; i + + )
        {
            string str = inputstr.Substring(i, 1);
            byte[] Chinesecodearry = System.Text.Encoding.GetEncoding(936).GetBytes(str);
            int codelen = (byte)Chinesecodearry.Length;
            if (codelen == 1)
            {
                hexcode = hexcode + str;
            }
            else
            {
                hexcode = hexcode + "\x" + Chinesecodearry[0].ToString("X2") + Chinesecodearry[1].ToString("X2");
            }
        }
        return hexcode;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}