• 刷卡客户端实现


    刷卡客户端实现

    客户端界面效果图:

    2.客户端功能实现

    一:窗体代码

      1 namespace WriteCard
      2 {
      3     partial class FrmCard
      4     {
      5         /// <summary>
      6         /// 必需的设计器变量。
      7         /// </summary>
      8         private System.ComponentModel.IContainer components = null;
      9 
     10         /// <summary>
     11         /// 清理所有正在使用的资源。
     12         /// </summary>
     13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
     14         protected override void Dispose(bool disposing)
     15         {
     16             if (disposing && (components != null))
     17             {
     18                 components.Dispose();
     19             }
     20             base.Dispose(disposing);
     21         }
     22 
     23         #region Windows 窗体设计器生成的代码
     24 
     25         /// <summary>
     26         /// 设计器支持所需的方法 - 不要
     27         /// 使用代码编辑器修改此方法的内容。
     28         /// </summary>
     29         private void InitializeComponent()
     30         {
     31             this.btnCard = new System.Windows.Forms.Button();
     32             this.txtCardNo = new System.Windows.Forms.TextBox();
     33             this.label1 = new System.Windows.Forms.Label();
     34             this.label2 = new System.Windows.Forms.Label();
     35             this.lblShuaCardtime = new System.Windows.Forms.Label();
     36             this.groupBox1 = new System.Windows.Forms.GroupBox();
     37             this.label8 = new System.Windows.Forms.Label();
     38             this.label9 = new System.Windows.Forms.Label();
     39             this.label10 = new System.Windows.Forms.Label();
     40             this.txtSchool = new System.Windows.Forms.TextBox();
     41             this.txtIDCard = new System.Windows.Forms.TextBox();
     42             this.txtPosition = new System.Windows.Forms.TextBox();
     43             this.label3 = new System.Windows.Forms.Label();
     44             this.label6 = new System.Windows.Forms.Label();
     45             this.label5 = new System.Windows.Forms.Label();
     46             this.label4 = new System.Windows.Forms.Label();
     47             this.txtClass = new System.Windows.Forms.TextBox();
     48             this.txtSex = new System.Windows.Forms.TextBox();
     49             this.txtName = new System.Windows.Forms.TextBox();
     50             this.txtCard = new System.Windows.Forms.TextBox();
     51             this.menuStrip1 = new System.Windows.Forms.MenuStrip();
     52             this.信息设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     53             this.groupBox1.SuspendLayout();
     54             this.menuStrip1.SuspendLayout();
     55             this.SuspendLayout();
     56             // 
     57             // btnCard
     58             // 
     59             this.btnCard.Location = new System.Drawing.Point(301, 39);
     60             this.btnCard.Name = "btnCard";
     61             this.btnCard.Size = new System.Drawing.Size(75, 23);
     62             this.btnCard.TabIndex = 0;
     63             this.btnCard.Text = "刷卡";
     64             this.btnCard.UseVisualStyleBackColor = true;
     65             this.btnCard.Click += new System.EventHandler(this.btnCard_Click);
     66             // 
     67             // txtCardNo
     68             // 
     69             this.txtCardNo.Location = new System.Drawing.Point(78, 39);
     70             this.txtCardNo.Name = "txtCardNo";
     71             this.txtCardNo.Size = new System.Drawing.Size(205, 21);
     72             this.txtCardNo.TabIndex = 1;
     73             // 
     74             // label1
     75             // 
     76             this.label1.AutoSize = true;
     77             this.label1.Location = new System.Drawing.Point(29, 42);
     78             this.label1.Name = "label1";
     79             this.label1.Size = new System.Drawing.Size(41, 12);
     80             this.label1.TabIndex = 7;
     81             this.label1.Text = "卡号:";
     82             // 
     83             // label2
     84             // 
     85             this.label2.AutoSize = true;
     86             this.label2.Location = new System.Drawing.Point(29, 66);
     87             this.label2.Name = "label2";
     88             this.label2.Size = new System.Drawing.Size(65, 12);
     89             this.label2.TabIndex = 8;
     90             this.label2.Text = "刷卡时间:";
     91             // 
     92             // lblShuaCardtime
     93             // 
     94             this.lblShuaCardtime.AutoSize = true;
     95             this.lblShuaCardtime.Location = new System.Drawing.Point(100, 66);
     96             this.lblShuaCardtime.Name = "lblShuaCardtime";
     97             this.lblShuaCardtime.Size = new System.Drawing.Size(41, 12);
     98             this.lblShuaCardtime.TabIndex = 9;
     99             this.lblShuaCardtime.Text = "******";
    100             // 
    101             // groupBox1
    102             // 
    103             this.groupBox1.Controls.Add(this.label8);
    104             this.groupBox1.Controls.Add(this.label9);
    105             this.groupBox1.Controls.Add(this.label10);
    106             this.groupBox1.Controls.Add(this.txtSchool);
    107             this.groupBox1.Controls.Add(this.txtIDCard);
    108             this.groupBox1.Controls.Add(this.txtPosition);
    109             this.groupBox1.Controls.Add(this.label3);
    110             this.groupBox1.Controls.Add(this.label6);
    111             this.groupBox1.Controls.Add(this.label5);
    112             this.groupBox1.Controls.Add(this.label4);
    113             this.groupBox1.Controls.Add(this.txtClass);
    114             this.groupBox1.Controls.Add(this.txtSex);
    115             this.groupBox1.Controls.Add(this.txtName);
    116             this.groupBox1.Controls.Add(this.txtCard);
    117             this.groupBox1.Location = new System.Drawing.Point(31, 87);
    118             this.groupBox1.Name = "groupBox1";
    119             this.groupBox1.Size = new System.Drawing.Size(252, 228);
    120             this.groupBox1.TabIndex = 13;
    121             this.groupBox1.TabStop = false;
    122             this.groupBox1.Text = "人员信息";
    123             // 
    124             // label8
    125             // 
    126             this.label8.AutoSize = true;
    127             this.label8.Location = new System.Drawing.Point(8, 192);
    128             this.label8.Name = "label8";
    129             this.label8.Size = new System.Drawing.Size(65, 12);
    130             this.label8.TabIndex = 27;
    131             this.label8.Text = "所属学校:";
    132             // 
    133             // label9
    134             // 
    135             this.label9.AutoSize = true;
    136             this.label9.Location = new System.Drawing.Point(10, 163);
    137             this.label9.Name = "label9";
    138             this.label9.Size = new System.Drawing.Size(65, 12);
    139             this.label9.TabIndex = 26;
    140             this.label9.Text = "身份证号:";
    141             // 
    142             // label10
    143             // 
    144             this.label10.AutoSize = true;
    145             this.label10.Location = new System.Drawing.Point(22, 136);
    146             this.label10.Name = "label10";
    147             this.label10.Size = new System.Drawing.Size(41, 12);
    148             this.label10.TabIndex = 25;
    149             this.label10.Text = "职称:";
    150             // 
    151             // txtSchool
    152             // 
    153             this.txtSchool.Location = new System.Drawing.Point(71, 187);
    154             this.txtSchool.Name = "txtSchool";
    155             this.txtSchool.Size = new System.Drawing.Size(159, 21);
    156             this.txtSchool.TabIndex = 23;
    157             // 
    158             // txtIDCard
    159             // 
    160             this.txtIDCard.Location = new System.Drawing.Point(71, 160);
    161             this.txtIDCard.Name = "txtIDCard";
    162             this.txtIDCard.Size = new System.Drawing.Size(159, 21);
    163             this.txtIDCard.TabIndex = 22;
    164             // 
    165             // txtPosition
    166             // 
    167             this.txtPosition.Location = new System.Drawing.Point(71, 133);
    168             this.txtPosition.Name = "txtPosition";
    169             this.txtPosition.Size = new System.Drawing.Size(159, 21);
    170             this.txtPosition.TabIndex = 21;
    171             // 
    172             // label3
    173             // 
    174             this.label3.AutoSize = true;
    175             this.label3.Location = new System.Drawing.Point(19, 106);
    176             this.label3.Name = "label3";
    177             this.label3.Size = new System.Drawing.Size(41, 12);
    178             this.label3.TabIndex = 20;
    179             this.label3.Text = "班级:";
    180             // 
    181             // label6
    182             // 
    183             this.label6.AutoSize = true;
    184             this.label6.Location = new System.Drawing.Point(22, 81);
    185             this.label6.Name = "label6";
    186             this.label6.Size = new System.Drawing.Size(35, 12);
    187             this.label6.TabIndex = 19;
    188             this.label6.Text = "性别:";
    189             // 
    190             // label5
    191             // 
    192             this.label5.AutoSize = true;
    193             this.label5.Location = new System.Drawing.Point(22, 52);
    194             this.label5.Name = "label5";
    195             this.label5.Size = new System.Drawing.Size(41, 12);
    196             this.label5.TabIndex = 18;
    197             this.label5.Text = "姓名:";
    198             // 
    199             // label4
    200             // 
    201             this.label4.AutoSize = true;
    202             this.label4.Location = new System.Drawing.Point(22, 25);
    203             this.label4.Name = "label4";
    204             this.label4.Size = new System.Drawing.Size(41, 12);
    205             this.label4.TabIndex = 17;
    206             this.label4.Text = "卡号:";
    207             // 
    208             // txtClass
    209             // 
    210             this.txtClass.Location = new System.Drawing.Point(71, 103);
    211             this.txtClass.Name = "txtClass";
    212             this.txtClass.Size = new System.Drawing.Size(159, 21);
    213             this.txtClass.TabIndex = 16;
    214             // 
    215             // txtSex
    216             // 
    217             this.txtSex.Location = new System.Drawing.Point(71, 76);
    218             this.txtSex.Name = "txtSex";
    219             this.txtSex.Size = new System.Drawing.Size(159, 21);
    220             this.txtSex.TabIndex = 15;
    221             // 
    222             // txtName
    223             // 
    224             this.txtName.Location = new System.Drawing.Point(71, 49);
    225             this.txtName.Name = "txtName";
    226             this.txtName.Size = new System.Drawing.Size(159, 21);
    227             this.txtName.TabIndex = 14;
    228             // 
    229             // txtCard
    230             // 
    231             this.txtCard.Location = new System.Drawing.Point(71, 22);
    232             this.txtCard.Name = "txtCard";
    233             this.txtCard.Size = new System.Drawing.Size(159, 21);
    234             this.txtCard.TabIndex = 13;
    235             // 
    236             // menuStrip1
    237             // 
    238             this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    239             this.信息设置ToolStripMenuItem});
    240             this.menuStrip1.Location = new System.Drawing.Point(0, 0);
    241             this.menuStrip1.Name = "menuStrip1";
    242             this.menuStrip1.Size = new System.Drawing.Size(380, 24);
    243             this.menuStrip1.TabIndex = 14;
    244             this.menuStrip1.Text = "menuStrip1";
    245             // 
    246             // 信息设置ToolStripMenuItem
    247             // 
    248             this.信息设置ToolStripMenuItem.Name = "信息设置ToolStripMenuItem";
    249             this.信息设置ToolStripMenuItem.Size = new System.Drawing.Size(65, 20);
    250             this.信息设置ToolStripMenuItem.Text = "信息设置";
    251             // 
    252             // FrmCard
    253             // 
    254             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    255             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    256             this.ClientSize = new System.Drawing.Size(380, 322);
    257             this.Controls.Add(this.groupBox1);
    258             this.Controls.Add(this.lblShuaCardtime);
    259             this.Controls.Add(this.label2);
    260             this.Controls.Add(this.label1);
    261             this.Controls.Add(this.txtCardNo);
    262             this.Controls.Add(this.btnCard);
    263             this.Controls.Add(this.menuStrip1);
    264             this.MainMenuStrip = this.menuStrip1;
    265             this.Name = "FrmCard";
    266             this.Text = "IC卡考勤刷卡";
    267             this.groupBox1.ResumeLayout(false);
    268             this.groupBox1.PerformLayout();
    269             this.menuStrip1.ResumeLayout(false);
    270             this.menuStrip1.PerformLayout();
    271             this.ResumeLayout(false);
    272             this.PerformLayout();
    273 
    274         }
    275 
    276         #endregion
    277 
    278         private System.Windows.Forms.Button btnCard;
    279         private System.Windows.Forms.TextBox txtCardNo;
    280         private System.Windows.Forms.Label label1;
    281         private System.Windows.Forms.Label label2;
    282         private System.Windows.Forms.Label lblShuaCardtime;
    283         private System.Windows.Forms.GroupBox groupBox1;
    284         private System.Windows.Forms.Label label6;
    285         private System.Windows.Forms.Label label5;
    286         private System.Windows.Forms.Label label4;
    287         private System.Windows.Forms.TextBox txtClass;
    288         private System.Windows.Forms.TextBox txtSex;
    289         private System.Windows.Forms.TextBox txtName;
    290         private System.Windows.Forms.TextBox txtCard;
    291         private System.Windows.Forms.Label label8;
    292         private System.Windows.Forms.Label label9;
    293         private System.Windows.Forms.Label label10;
    294         private System.Windows.Forms.TextBox txtSchool;
    295         private System.Windows.Forms.TextBox txtIDCard;
    296         private System.Windows.Forms.TextBox txtPosition;
    297         private System.Windows.Forms.Label label3;
    298         private System.Windows.Forms.MenuStrip menuStrip1;
    299         private System.Windows.Forms.ToolStripMenuItem 信息设置ToolStripMenuItem;
    300     }
    301 }
    View Code

    二:后台代码

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.IO;
     10 using System.Net;
     11 using Newtonsoft.Json;
     12 using System.Runtime.Serialization.Json;
     13 using System.Runtime.Serialization;
     14 namespace WriteCard
     15 {
     16     public partial class FrmCard : Form
     17     {
     18         string strURL = "http://localhost:15402/Handler1.ashx";
     19         public FrmCard()
     20         {
     21             InitializeComponent();
     22         }
     23         /// <summary>
     24         /// 刷卡操作
     25         /// </summary>
     26         /// <param name="sender"></param>
     27         /// <param name="e"></param>
     28         private void btnCard_Click(object sender, EventArgs e)
     29         {
     30             this.lblShuaCardtime.Text = DateTime.Now.ToString();
     31             string strCardNo = this.txtCardNo.Text;//卡号变量
     32             string postData = strCardNo;//要提交的数据
     33             GetPage(strURL, postData);
     34             //定义返回json数据 解析json数据并显示信息到窗体控件值上
     35             string jsoncontent = GetPage(strURL, postData);
     36             //string html_custerm = HttpHelper.GetResponse(jsoncontent, "get", string.Empty);
     37             //string str_custerm = html_custerm.Replace("cnp([", "").Replace("]);", "");
     38             //Custerm custerm = new Custerm();
     39            // custerm = JsonConvert.DeserializeObject(jsoncontent, typeof(Custerm)) as Custerm;
     40            // this.txtCard.Text = custerm.CardNO;
     41             List<Custerm> CustermList = JsonConvert.DeserializeObject<List<Custerm>>(jsoncontent);
     42 
     43             foreach (Custerm custerm in CustermList)
     44             {
     45                 //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);
     46                 this.txtCard.Text = custerm.CardNO;
     47                 this.txtName.Text = custerm.CustermName;
     48                 this.txtSex.Text = custerm.Sex;
     49                 this.txtClass.Text = custerm.ClassName;
     50                 this.txtPosition.Text = custerm.Professional;
     51                 this.txtIDCard.Text = custerm.SIDCard;
     52                 this.txtSchool.Text = custerm.SchoolName;
     53             }  
     54             
     55         }
     56 
     57         /// <summary>  
     58         /// Post数据到web服务端  
     59         /// </summary>  
     60         /// <param name="strURL">网址</param>  
     61         /// <param name="postData">参数</param>  
     62         /// <returns></returns>  
     63         public string GetPage(string strURL, string postData)
     64         {          
     65             Stream outstream = null;
     66             Stream instream = null;
     67             StreamReader sr = null;
     68             HttpWebResponse response = null;
     69             HttpWebRequest request = null;
     70             Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
     71             //postData = postData+""}";
     72             byte[] data = encoding.GetBytes(postData);
     73             // 准备请求...  
     74             try
     75             {
     76                 // 设置参数  
     77                 request = WebRequest.Create(strURL) as HttpWebRequest;
     78                 CookieContainer cookieContainer = new CookieContainer();
     79                 request.CookieContainer = cookieContainer;
     80                 request.AllowAutoRedirect = true;
     81                 //Post请求方式
     82                 request.Method = "POST";
     83                 request.KeepAlive = false;
     84                 //内容类型
     85                 request.ContentType = "application/x-www-form-urlencoded";
     86                 request.ContentLength = data.Length;
     87                 outstream = request.GetRequestStream();
     88                 //send the data
     89                 outstream.Write(data, 0, data.Length);
     90                 outstream.Close();
     91                 //发送请求并获取相应回应数据  
     92                 response = request.GetResponse() as HttpWebResponse;
     93                 //直到request.GetResponse()程序才开始向目标网页发送Post请求  
     94                 instream = response.GetResponseStream();//获得请求流
     95                 sr = new StreamReader(instream, encoding);
     96                 //返回结果网页(html)代码  
     97                 string content = sr.ReadToEnd();
     98                 Console.WriteLine(content); 
     99                 string err = string.Empty;
    100                 return content;
    101 
    102             }
    103             catch (Exception ex)
    104             {
    105                 string err = ex.Message;
    106                 return string.Empty;
    107             }
    108         }       
    109         
    110         /// <summary>
    111         /// 人员信息类
    112         /// </summary>       
    113       public  class Custerm
    114         {
    115             public string CustermID { get; set; }
    116             public string CustermName { get; set; }
    117             public string CardNO { get; set; }
    118             public string ClassName { get; set; }
    119             public string Sex { get; set; }
    120             public string SIDCard { get; set; }
    121             public string SchoolName { get; set; }
    122             public string HomeAddress { get; set; }
    123             public string Professional { get; set; }
    124 
    125             //public Custerm(string CustermID, string CustermName, string CardNO, string ClassName, string Sex, string SIDCard, string SchoolName, string HomeAddress, string Professional)
    126             //{
    127             //    this.CustermID = CustermID;
    128             //    this.CustermName = CustermName;
    129             //    this.CardNO = CardNO;
    130             //    this.ClassName = ClassName;
    131             //    this.Sex = Sex;
    132             //    this.SIDCard = SIDCard;
    133             //    this.SchoolName = SchoolName;
    134             //    this.HomeAddress = HomeAddress;
    135             //    this.Professional = Professional;
    136             //}
    137 
    138         }
    139      
    140     }
    141 }
    View Code

    三:引用类库 Newtonsoft.Json.dll

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Net;
      6 using System.IO;
      7 using System.Text.RegularExpressions;
      8 
      9 namespace WriteCard
     10 {
     11     
     12 public class HttpHelper
     13     {
     14         public static CookieContainer CookieContainers = new CookieContainer();
     15 
     16         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";
     17         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)";
     18 
     19         /// <summary>
     20         /// 
     21         /// </summary>
     22         /// <param name="url"></param>
     23         /// <param name="method">"POST" or "GET"</param>
     24         /// <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>
     25         /// <returns></returns>
     26         public static string GetResponse(string url, string method, string data)
     27         {
     28             try
     29             {
     30                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
     31                 req.KeepAlive = true;
     32                 req.Method = method.ToUpper();
     33                 req.AllowAutoRedirect = true;
     34                 req.CookieContainer = CookieContainers;
     35                 req.ContentType = "application/x-www-form-urlencoded";
     36 
     37                 req.UserAgent = IE7;
     38                 req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
     39                 req.Timeout = 50000;
     40 
     41                 if (method.ToUpper() == "POST" && data != null)
     42                 {
     43                     ASCIIEncoding encoding = new ASCIIEncoding();
     44                     byte[] postBytes = encoding.GetBytes(data); ;
     45                     req.ContentLength = postBytes.Length;
     46                     Stream st = req.GetRequestStream();
     47                     st.Write(postBytes, 0, postBytes.Length);
     48                     st.Close();
     49                 }
     50 
     51                 System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
     52                 {
     53                     return true;
     54                 };
     55 
     56                 //Encoding myEncoding = Encoding.GetEncoding("UTF-8");
     57                 Encoding myEncoding = Encoding.GetEncoding("gb2312");
     58 
     59                 HttpWebResponse res = (HttpWebResponse)req.GetResponse();
     60                 Stream resst = res.GetResponseStream();
     61                 StreamReader sr = new StreamReader(resst, myEncoding);
     62                 string str = sr.ReadToEnd();
     63 
     64                 return str;
     65             }
     66             catch (Exception)
     67             {
     68                 return string.Empty;
     69             }
     70         }
     71 
     72         public static Stream GetResponseImage(string url)
     73         {
     74             Stream resst = null;
     75             try
     76             {
     77                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
     78                 req.KeepAlive = true;
     79                 req.Method = "GET";
     80                 req.AllowAutoRedirect = true;
     81                 req.CookieContainer = CookieContainers;
     82                 req.ContentType = "application/x-www-form-urlencoded";
     83 
     84                 req.UserAgent = IE7;
     85                 req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
     86                 req.Timeout = 50000;
     87 
     88                 System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
     89                 {
     90                     return true;
     91                 };
     92 
     93                 Encoding myEncoding = Encoding.GetEncoding("UTF-8");
     94 
     95                 HttpWebResponse res = (HttpWebResponse)req.GetResponse();
     96                 resst = res.GetResponseStream();
     97 
     98                 return resst;
     99             }
    100             catch
    101             {
    102                 return null;
    103             }
    104         }
    105 
    106         public static string GetRegexString(string pattern, string source)
    107         {
    108             Regex r = new Regex(pattern);
    109             MatchCollection mc = r.Matches(source);
    110             return mc[0].Groups[1].Value;
    111         }
    112 
    113         public static string[] GetRegexStrings(string pattern, string source)
    114         {
    115             Regex r = new Regex(pattern);
    116             MatchCollection mcs = r.Matches(source);
    117 
    118             string[] ret = new string[mcs.Count];
    119 
    120             for (int i = 0; i < mcs.Count; i++)
    121                 ret[i] = mcs[i].Groups[1].Value;
    122 
    123             return ret;
    124         }
    125     }
    126 }
    View Code
  • 相关阅读:
    查看数据库所有的表
    oracle JOB学习(一)---基础
    图片实时预览JSP加js
    Collections.sort()
    FileUtil.java
    设计模式:常见类的关系
    枚举
    相册
    jQuery----blur()方法
    上传文件详解
  • 原文地址:https://www.cnblogs.com/ruishuang208/p/4193141.html
Copyright © 2020-2023  润新知