zoukankan      html  css  js  c++  java
  • 刷卡客户端实现

    客户端界面效果图:

     

    2.客户端功能实现

    一:窗体代码

    namespace WriteCard
    {
        partial class FrmCard
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.btnCard = new System.Windows.Forms.Button();
                this.txtCardNo = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.lblShuaCardtime = new System.Windows.Forms.Label();
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.label8 = new System.Windows.Forms.Label();
                this.label9 = new System.Windows.Forms.Label();
                this.label10 = new System.Windows.Forms.Label();
                this.txtSchool = new System.Windows.Forms.TextBox();
                this.txtIDCard = new System.Windows.Forms.TextBox();
                this.txtPosition = new System.Windows.Forms.TextBox();
                this.label3 = new System.Windows.Forms.Label();
                this.label6 = new System.Windows.Forms.Label();
                this.label5 = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.txtClass = new System.Windows.Forms.TextBox();
                this.txtSex = new System.Windows.Forms.TextBox();
                this.txtName = new System.Windows.Forms.TextBox();
                this.txtCard = new System.Windows.Forms.TextBox();
                this.menuStrip1 = new System.Windows.Forms.MenuStrip();
                this.信息设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
                this.groupBox1.SuspendLayout();
                this.menuStrip1.SuspendLayout();
                this.SuspendLayout();
                // 
                // btnCard
                // 
                this.btnCard.Location = new System.Drawing.Point(301, 39);
                this.btnCard.Name = "btnCard";
                this.btnCard.Size = new System.Drawing.Size(75, 23);
                this.btnCard.TabIndex = 0;
                this.btnCard.Text = "刷卡";
                this.btnCard.UseVisualStyleBackColor = true;
                this.btnCard.Click += new System.EventHandler(this.btnCard_Click);
                // 
                // txtCardNo
                // 
                this.txtCardNo.Location = new System.Drawing.Point(78, 39);
                this.txtCardNo.Name = "txtCardNo";
                this.txtCardNo.Size = new System.Drawing.Size(205, 21);
                this.txtCardNo.TabIndex = 1;
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(29, 42);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(41, 12);
                this.label1.TabIndex = 7;
                this.label1.Text = "卡号:";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(29, 66);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(65, 12);
                this.label2.TabIndex = 8;
                this.label2.Text = "刷卡时间:";
                // 
                // lblShuaCardtime
                // 
                this.lblShuaCardtime.AutoSize = true;
                this.lblShuaCardtime.Location = new System.Drawing.Point(100, 66);
                this.lblShuaCardtime.Name = "lblShuaCardtime";
                this.lblShuaCardtime.Size = new System.Drawing.Size(41, 12);
                this.lblShuaCardtime.TabIndex = 9;
                this.lblShuaCardtime.Text = "******";
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.label8);
                this.groupBox1.Controls.Add(this.label9);
                this.groupBox1.Controls.Add(this.label10);
                this.groupBox1.Controls.Add(this.txtSchool);
                this.groupBox1.Controls.Add(this.txtIDCard);
                this.groupBox1.Controls.Add(this.txtPosition);
                this.groupBox1.Controls.Add(this.label3);
                this.groupBox1.Controls.Add(this.label6);
                this.groupBox1.Controls.Add(this.label5);
                this.groupBox1.Controls.Add(this.label4);
                this.groupBox1.Controls.Add(this.txtClass);
                this.groupBox1.Controls.Add(this.txtSex);
                this.groupBox1.Controls.Add(this.txtName);
                this.groupBox1.Controls.Add(this.txtCard);
                this.groupBox1.Location = new System.Drawing.Point(31, 87);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(252, 228);
                this.groupBox1.TabIndex = 13;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "人员信息";
                // 
                // label8
                // 
                this.label8.AutoSize = true;
                this.label8.Location = new System.Drawing.Point(8, 192);
                this.label8.Name = "label8";
                this.label8.Size = new System.Drawing.Size(65, 12);
                this.label8.TabIndex = 27;
                this.label8.Text = "所属学校:";
                // 
                // label9
                // 
                this.label9.AutoSize = true;
                this.label9.Location = new System.Drawing.Point(10, 163);
                this.label9.Name = "label9";
                this.label9.Size = new System.Drawing.Size(65, 12);
                this.label9.TabIndex = 26;
                this.label9.Text = "身份证号:";
                // 
                // label10
                // 
                this.label10.AutoSize = true;
                this.label10.Location = new System.Drawing.Point(22, 136);
                this.label10.Name = "label10";
                this.label10.Size = new System.Drawing.Size(41, 12);
                this.label10.TabIndex = 25;
                this.label10.Text = "职称:";
                // 
                // txtSchool
                // 
                this.txtSchool.Location = new System.Drawing.Point(71, 187);
                this.txtSchool.Name = "txtSchool";
                this.txtSchool.Size = new System.Drawing.Size(159, 21);
                this.txtSchool.TabIndex = 23;
                // 
                // txtIDCard
                // 
                this.txtIDCard.Location = new System.Drawing.Point(71, 160);
                this.txtIDCard.Name = "txtIDCard";
                this.txtIDCard.Size = new System.Drawing.Size(159, 21);
                this.txtIDCard.TabIndex = 22;
                // 
                // txtPosition
                // 
                this.txtPosition.Location = new System.Drawing.Point(71, 133);
                this.txtPosition.Name = "txtPosition";
                this.txtPosition.Size = new System.Drawing.Size(159, 21);
                this.txtPosition.TabIndex = 21;
                // 
                // label3
                // 
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(19, 106);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(41, 12);
                this.label3.TabIndex = 20;
                this.label3.Text = "班级:";
                // 
                // label6
                // 
                this.label6.AutoSize = true;
                this.label6.Location = new System.Drawing.Point(22, 81);
                this.label6.Name = "label6";
                this.label6.Size = new System.Drawing.Size(35, 12);
                this.label6.TabIndex = 19;
                this.label6.Text = "性别:";
                // 
                // label5
                // 
                this.label5.AutoSize = true;
                this.label5.Location = new System.Drawing.Point(22, 52);
                this.label5.Name = "label5";
                this.label5.Size = new System.Drawing.Size(41, 12);
                this.label5.TabIndex = 18;
                this.label5.Text = "姓名:";
                // 
                // label4
                // 
                this.label4.AutoSize = true;
                this.label4.Location = new System.Drawing.Point(22, 25);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(41, 12);
                this.label4.TabIndex = 17;
                this.label4.Text = "卡号:";
                // 
                // txtClass
                // 
                this.txtClass.Location = new System.Drawing.Point(71, 103);
                this.txtClass.Name = "txtClass";
                this.txtClass.Size = new System.Drawing.Size(159, 21);
                this.txtClass.TabIndex = 16;
                // 
                // txtSex
                // 
                this.txtSex.Location = new System.Drawing.Point(71, 76);
                this.txtSex.Name = "txtSex";
                this.txtSex.Size = new System.Drawing.Size(159, 21);
                this.txtSex.TabIndex = 15;
                // 
                // txtName
                // 
                this.txtName.Location = new System.Drawing.Point(71, 49);
                this.txtName.Name = "txtName";
                this.txtName.Size = new System.Drawing.Size(159, 21);
                this.txtName.TabIndex = 14;
                // 
                // txtCard
                // 
                this.txtCard.Location = new System.Drawing.Point(71, 22);
                this.txtCard.Name = "txtCard";
                this.txtCard.Size = new System.Drawing.Size(159, 21);
                this.txtCard.TabIndex = 13;
                // 
                // menuStrip1
                // 
                this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.信息设置ToolStripMenuItem});
                this.menuStrip1.Location = new System.Drawing.Point(0, 0);
                this.menuStrip1.Name = "menuStrip1";
                this.menuStrip1.Size = new System.Drawing.Size(380, 24);
                this.menuStrip1.TabIndex = 14;
                this.menuStrip1.Text = "menuStrip1";
                // 
                // 信息设置ToolStripMenuItem
                // 
                this.信息设置ToolStripMenuItem.Name = "信息设置ToolStripMenuItem";
                this.信息设置ToolStripMenuItem.Size = new System.Drawing.Size(65, 20);
                this.信息设置ToolStripMenuItem.Text = "信息设置";
                // 
                // FrmCard
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(380, 322);
                this.Controls.Add(this.groupBox1);
                this.Controls.Add(this.lblShuaCardtime);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.txtCardNo);
                this.Controls.Add(this.btnCard);
                this.Controls.Add(this.menuStrip1);
                this.MainMenuStrip = this.menuStrip1;
                this.Name = "FrmCard";
                this.Text = "IC卡考勤刷卡";
                this.groupBox1.ResumeLayout(false);
                this.groupBox1.PerformLayout();
                this.menuStrip1.ResumeLayout(false);
                this.menuStrip1.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Button btnCard;
            private System.Windows.Forms.TextBox txtCardNo;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label lblShuaCardtime;
            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.Label label6;
            private System.Windows.Forms.Label label5;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.TextBox txtClass;
            private System.Windows.Forms.TextBox txtSex;
            private System.Windows.Forms.TextBox txtName;
            private System.Windows.Forms.TextBox txtCard;
            private System.Windows.Forms.Label label8;
            private System.Windows.Forms.Label label9;
            private System.Windows.Forms.Label label10;
            private System.Windows.Forms.TextBox txtSchool;
            private System.Windows.Forms.TextBox txtIDCard;
            private System.Windows.Forms.TextBox txtPosition;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.MenuStrip menuStrip1;
            private System.Windows.Forms.ToolStripMenuItem 信息设置ToolStripMenuItem;
        }
    }
    View Code

    二:后台代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using Newtonsoft.Json;
    using System.Runtime.Serialization.Json;
    using System.Runtime.Serialization;
    namespace WriteCard
    {
        public partial class FrmCard : Form
        {
            string strURL = "http://localhost:15402/Handler1.ashx";
            public FrmCard()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 刷卡操作
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnCard_Click(object sender, EventArgs e)
            {
                this.lblShuaCardtime.Text = DateTime.Now.ToString();
                string strCardNo = this.txtCardNo.Text;//卡号变量
                string postData = strCardNo;//要提交的数据
                GetPage(strURL, postData);
                //定义返回json数据 解析json数据并显示信息到窗体控件值上
                string jsoncontent = GetPage(strURL, postData);
                //string html_custerm = HttpHelper.GetResponse(jsoncontent, "get", string.Empty);
                //string str_custerm = html_custerm.Replace("cnp([", "").Replace("]);", "");
                //Custerm custerm = new Custerm();
               // custerm = JsonConvert.DeserializeObject(jsoncontent, typeof(Custerm)) as Custerm;
               // this.txtCard.Text = custerm.CardNO;
                List<Custerm> CustermList = JsonConvert.DeserializeObject<List<Custerm>>(jsoncontent);
    
                foreach (Custerm custerm in CustermList)
                {
                    //Console.WriteLine("UserName:" + custerm.CustermName + "UserID:" + custerm.CustermID + "CardNO:" + custerm.CardNO + "ClassName:" + custerm.ClassName + "HomeAddress:" + custerm.HomeAddress + "Professional:" + custerm.Professional + "SchoolName:" + custerm.SchoolName + "Sex:" + custerm.Sex + "SIDCard:" + custerm.SIDCard);
                    this.txtCard.Text = custerm.CardNO;
                    this.txtName.Text = custerm.CustermName;
                    this.txtSex.Text = custerm.Sex;
                    this.txtClass.Text = custerm.ClassName;
                    this.txtPosition.Text = custerm.Professional;
                    this.txtIDCard.Text = custerm.SIDCard;
                    this.txtSchool.Text = custerm.SchoolName;
                }  
                
            }
    
            /// <summary>  
            /// Post数据到web服务端  
            /// </summary>  
            /// <param name="strURL">网址</param>  
            /// <param name="postData">参数</param>  
            /// <returns></returns>  
            public string GetPage(string strURL, string postData)
            {          
                Stream outstream = null;
                Stream instream = null;
                StreamReader sr = null;
                HttpWebResponse response = null;
                HttpWebRequest request = null;
                Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
                //postData = postData+""}";
                byte[] data = encoding.GetBytes(postData);
                // 准备请求...  
                try
                {
                    // 设置参数  
                    request = WebRequest.Create(strURL) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    //Post请求方式
                    request.Method = "POST";
                    request.KeepAlive = false;
                    //内容类型
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    outstream = request.GetRequestStream();
                    //send the data
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();
                    //发送请求并获取相应回应数据  
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求  
                    instream = response.GetResponseStream();//获得请求流
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码  
                    string content = sr.ReadToEnd();
                    Console.WriteLine(content); 
                    string err = string.Empty;
                    return content;
    
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    return string.Empty;
                }
            }       
            
            /// <summary>
            /// 人员信息类
            /// </summary>       
          public  class Custerm
            {
                public string CustermID { get; set; }
                public string CustermName { get; set; }
                public string CardNO { get; set; }
                public string ClassName { get; set; }
                public string Sex { get; set; }
                public string SIDCard { get; set; }
                public string SchoolName { get; set; }
                public string HomeAddress { get; set; }
                public string Professional { get; set; }
    
                //public Custerm(string CustermID, string CustermName, string CardNO, string ClassName, string Sex, string SIDCard, string SchoolName, string HomeAddress, string Professional)
                //{
                //    this.CustermID = CustermID;
                //    this.CustermName = CustermName;
                //    this.CardNO = CardNO;
                //    this.ClassName = ClassName;
                //    this.Sex = Sex;
                //    this.SIDCard = SIDCard;
                //    this.SchoolName = SchoolName;
                //    this.HomeAddress = HomeAddress;
                //    this.Professional = Professional;
                //}
    
            }
         
        }
    }
    View Code

    三:引用类库 Newtonsoft.Json.dll

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace WriteCard
    {
        
    public class HttpHelper
        {
            public static CookieContainer CookieContainers = new CookieContainer();
    
            public static string FireFoxAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23";
            public static string IE7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E)";
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="url"></param>
            /// <param name="method">"POST" or "GET"</param>
            /// <param name="data">when the method is "POST", the data will send to web server, if the method is "GET", the data should be string.empty</param>
            /// <returns></returns>
            public static string GetResponse(string url, string method, string data)
            {
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.KeepAlive = true;
                    req.Method = method.ToUpper();
                    req.AllowAutoRedirect = true;
                    req.CookieContainer = CookieContainers;
                    req.ContentType = "application/x-www-form-urlencoded";
    
                    req.UserAgent = IE7;
                    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    req.Timeout = 50000;
    
                    if (method.ToUpper() == "POST" && data != null)
                    {
                        ASCIIEncoding encoding = new ASCIIEncoding();
                        byte[] postBytes = encoding.GetBytes(data); ;
                        req.ContentLength = postBytes.Length;
                        Stream st = req.GetRequestStream();
                        st.Write(postBytes, 0, postBytes.Length);
                        st.Close();
                    }
    
                    System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
                    {
                        return true;
                    };
    
                    //Encoding myEncoding = Encoding.GetEncoding("UTF-8");
                    Encoding myEncoding = Encoding.GetEncoding("gb2312");
    
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    Stream resst = res.GetResponseStream();
                    StreamReader sr = new StreamReader(resst, myEncoding);
                    string str = sr.ReadToEnd();
    
                    return str;
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
    
            public static Stream GetResponseImage(string url)
            {
                Stream resst = null;
                try
                {
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.KeepAlive = true;
                    req.Method = "GET";
                    req.AllowAutoRedirect = true;
                    req.CookieContainer = CookieContainers;
                    req.ContentType = "application/x-www-form-urlencoded";
    
                    req.UserAgent = IE7;
                    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    req.Timeout = 50000;
    
                    System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
                    {
                        return true;
                    };
    
                    Encoding myEncoding = Encoding.GetEncoding("UTF-8");
    
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    resst = res.GetResponseStream();
    
                    return resst;
                }
                catch
                {
                    return null;
                }
            }
    
            public static string GetRegexString(string pattern, string source)
            {
                Regex r = new Regex(pattern);
                MatchCollection mc = r.Matches(source);
                return mc[0].Groups[1].Value;
            }
    
            public static string[] GetRegexStrings(string pattern, string source)
            {
                Regex r = new Regex(pattern);
                MatchCollection mcs = r.Matches(source);
    
                string[] ret = new string[mcs.Count];
    
                for (int i = 0; i < mcs.Count; i++)
                    ret[i] = mcs[i].Groups[1].Value;
    
                return ret;
            }
        }
    }
    View Code
  • 相关阅读:
    python之private variable
    python实例、类方法、静态方法
    python常用option
    access
    FD_CLOEXEC
    fork后父子进程文件描述问题
    split
    信号
    kill
    进程组&Session
  • 原文地址:https://www.cnblogs.com/Warmsunshine/p/3587443.html
Copyright © 2011-2022 走看看