• 评论

C#控制台作为服务器接收消息

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MouseMoNi
{
    /// <summary>
    /// 如提示 HttpListener拒绝访问异常
    /// 以管理员CMD命令行执行:
    /// netsh http delete urlacl url=http://+:30000/mouse/ 
    /// netsh http add urlacl url=http://+:30000/mouse/  user=Everyone
    /// </summary>
    class Program
    {
        private static HttpListener httpPostRequest = new HttpListener();

        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        //移动鼠标 
        const int MOUSEEVENTF_MOVE = 0x0001;
        //模拟鼠标左键按下 
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        //模拟鼠标左键抬起 
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        //模拟鼠标右键按下 
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        //模拟鼠标右键抬起 
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        //模拟鼠标中键按下 
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        //模拟鼠标中键抬起 
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        //标示是否采用绝对坐标 
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        //模拟鼠标滚轮滚动操作,必须配合dwData参数
        const int MOUSEEVENTF_WHEEL = 0x0800;
        static void Main(string[] args)
        {
            httpPostRequest.Prefixes.Add("http://+:30000/mouse/");
            httpPostRequest.Start();

            Thread ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle));
            ThrednHttpPostRequest.Start();
        }
        public static void TestMoveMouse(int x, int y)
        {
            Console.WriteLine("点击x:" + x+",y:"+ y +","+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) ;
            mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x * 65535 / 1024, y * 65535 / 1280, 0, 0);//相对当前鼠标位置x轴和y轴分别移动50像素
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//模拟鼠标左键按下
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//模拟鼠标左键抬起
        }
        private static void httpPostRequestHandle()
        {
            try
            {
                while (true)
                {
                    HttpListenerContext requestContext = httpPostRequest.GetContext();
                    Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) =>
                    {
                        HttpListenerContext request = (HttpListenerContext)requestcontext;


                        ////获取Post请求中的参数和值帮助类
                        //HttpListenerPostParaHelper httppost = new HttpListenerPostParaHelper(request);
                        ////获取Post过来的参数和数据
                        //List<HttpListenerPostValue> lst = httppost.GetHttpListenerPostValue();

                        int x = 0;
                        int y = 0;
                        if (request.Request.QueryString["x"] != null)
                        {
                            try
                            {
                                x = int.Parse(request.Request.QueryString["x"]);
                            }
                            catch (Exception ex)
                            {
                                x = 0;
                            }
                        }
                        if (request.Request.QueryString["y"] != null)
                        {
                            try
                            {
                                y = int.Parse(request.Request.QueryString["y"]);
                            }
                            catch (Exception ex)
                            {
                                y = 0;
                            }
                        }
                        TestMoveMouse(x, y);
                        //Response
                        request.Response.StatusCode = 200;
                        request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                        request.Response.ContentType = "application/json; charset=utf-8";
                        requestContext.Response.ContentEncoding = Encoding.UTF8;
                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
                        //request.Response.ContentLength64 = buffer.Length;
                        var output = request.Response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);
                        output.Close();
                    }));
                    threadsub.Start(requestContext);
                }
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message +","+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            
        }
    }
}


相关文章