博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
中控考勤机WEB主动上报接收SERVER程序
阅读量:5763 次
发布时间:2019-06-18

本文共 4729 字,大约阅读时间需要 15 分钟。

using System;using System.IO;using System.Net;using System.Text.RegularExpressions;namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            using (var listerner = new HttpListener())            {                listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;                var ipEntry = Dns.GetHostEntry(Dns.GetHostName());                var ipList = ipEntry.AddressList;                const int port = 8088;                foreach (var ip in ipList)                {                    if (IsCorrenctIp(ip.ToString()))                    {                        listerner.Prefixes.Add("http://" + ip + ":"+ port + "/iclock/");                    }                }                listerner.Prefixes.Add("http://127.0.0.1:"+ port + "/iclock/");                listerner.Prefixes.Add("http://localhost:"+ port + "/iclock/");                listerner.Start();                Console.WriteLine("【系统提示】考勤管理系统启动成功!");                while (true)                {                    //等待请求连接                    //没有请求则GetContext处于阻塞状态                    var ctx = listerner.GetContext();                    ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码                    var sn = ctx.Request.QueryString["SN"];                    var httpMethod=ctx.Request.HttpMethod;                    var table = ctx.Request.QueryString["table"];                    var count = 1;                    if ((sn != null) &&(table!=null)&&table== "ATTLOG"&&(httpMethod=="POST"))                    {                        Console.WriteLine("设备号:"+sn);                                                var result = GetRequestPostData(ctx.Request, out count);                        var array = result.Split('\t');                        var userId = array[0];                        var userName = "未知人员";                        if (userId == "1")                        {                            userName = "黄海";                        }                        if (userId == "2")                        {                            userName = "吴缤";                        }                        if (userId == "3")                        {                            userName = "申健";                        }                        if (userId == "197710110")                        {                            userName = "周枫";                        }                        Console.WriteLine(userName + "    " + array[1]);                    }                                       //使用Writer输出http响应代码                    using (var writer = new StreamWriter(ctx.Response.OutputStream))                    {                        ctx.Response.ContentType = ctx.Request.AcceptTypes[0];                        writer.WriteLine("HTTP/1.1 200 OK"+"
"); writer.WriteLine("Server: DsidealSuperServer/1.9.0" + "
"); writer.WriteLine(DateTime.Now.ToString("r") + "
"); writer.WriteLine("Content-Type: text/plain" + "
"); writer.WriteLine("Connection: close" + "
"); writer.WriteLine("Content-Length: "+(3+count.ToString().Length)+ "
"); writer.WriteLine("Pragma: no-cache" + "
"); writer.WriteLine("Cache-Control: no-store" + "
"); writer.WriteLine("" + "
"); writer.WriteLine("OK:"+ count + "
"); writer.Close(); ctx.Response.Close(); } } } } public static bool IsCorrenctIp(string ip) { var pattrn = @"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])"; return Regex.IsMatch(ip, pattrn); } public static string GetRequestPostData(HttpListenerRequest request,out int Count) { Count = 0; if (!request.HasEntityBody) { return null; } var returnStr = ""; using (var body = request.InputStream) { using (var reader = new StreamReader(body, request.ContentEncoding)) { while (reader.Peek() >= 0) { var nowString = (char) reader.Read(); if (nowString.ToString() == "\n") { Count++; } returnStr = returnStr+ nowString; } } } return returnStr; } }}

 

转载地址:http://cfwux.baihongyu.com/

你可能感兴趣的文章
HTTP模块SuperAgent
查看>>
任务05—学习 MARKDOWN 语言
查看>>
测试使用Windows Live Writer写日志
查看>>
前端CSS框架
查看>>
Android入门(十三)内容提供器
查看>>
BINARY SEARCH in read table statement
查看>>
15LaTeX学习系列之---LaTeX里插入数学公式
查看>>
chainWebpack 和 htmlWebpackPlugin搭配使用
查看>>
20165231 结对编程项目_四则运算阶段总结
查看>>
17-ajax向后端提交POST请求
查看>>
查找linux下进程占用CPU过高的原因,以php-fpm为例
查看>>
linux 漏洞列表
查看>>
HttpWebRequest 保存Cookies,模拟Session登录
查看>>
js21点条件判断算法
查看>>
(四)java对象的结构和对象的访问定位
查看>>
springmvc 拦截器使用注意点
查看>>
SqlServer TimeOut服务器设置
查看>>
Java 代码性能优化总结
查看>>
Jira配置openLdap服务器进行用户认证
查看>>
正则表达式之match与exec【转的 楼兰之风】
查看>>