1. 程式人生 > >學習記錄---C# Web程序獲取客戶端電腦信息

學習記錄---C# Web程序獲取客戶端電腦信息

C# oid tostring -- mac listen console ads ima

問題描述:由於最近項目需要使用Mac地址與註冊碼進行加密處理,但是又因為Web程序的局限性不能獲取客戶端電腦系統信息,當然IE瀏覽器有一個activex控件他是可以通過Js在前端代碼中直接獲取的,局限性太小放棄。我的實現方法是通過windows服務嵌套一個HttpService服務實現。本人初級菜鳥希望有大佬看到能有多多指教,同時也記錄下來加深自己的印象。大概實現步驟如下

一 創建一個winform項目

二 在根目錄下創建一個windowsService服務名稱隨意,本文中取得是Service1

public partial class Service1 : ServiceBase
{
System.Threading.Timer recordTimer;

public Service1()
{
InitializeComponent();
}

/// <summary>
/// 服務開啟
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
//獲取Mac地址與註冊碼本地接口Http接口嵌入也可在增加其他業務代碼

HttpServer httpServer;
if (args.GetLength(0) > 0)
{
httpServer = new MyHttpServer(Convert.ToInt16(args[0]));
}
else
{

//日誌文件記錄
FileOpetation.SaveRecord(string.Format(@"當前記錄時間:{0},狀況:啟動服務!", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));

//爆的本機端口盡量用大的數字不要使用1433一下數字作為端口
httpServer = new MyHttpServer(8888);
}
//操作線程方法
Thread thread = new Thread(new ThreadStart(httpServer.listen));
thread.Start();
}

//停止服務

protected override void OnStop()
{
if (recordTimer != null)
{
recordTimer.Dispose();
}
}

三 創建一個服務類 實現.net框架的類HttpListener。以下為簡介代碼返回數據格式需要自行處理另外會存在一個跨域問題,代碼包中有註解

public class MyHttpServer : HttpServer {
    public MyHttpServer(int port)
        : base(port) {
    }
    public override void handleGETRequest(HttpProcessor p) {
        Console.WriteLine("request: {0}", p.http_url);
        p.writeSuccess();
        p.outputStream.WriteLine("<html><body><h1>test server</h1>");
        p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString());
        p.outputStream.WriteLine("url : {0}", p.http_url);
        
        p.outputStream.WriteLine("<form method=post action=/form>");
        p.outputStream.WriteLine("<input type=text name=foo value=foovalue>");
        p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>");
        p.outputStream.WriteLine("</form>");
    }
    
    public override void handlePOSTRequest(HttpProcessor p, StreamReader inputData) {
        Console.WriteLine("POST request: {0}", p.http_url);
        string data = inputData.ReadToEnd();
        
        p.outputStream.WriteLine("<html><body><h1>test server</h1>");
        p.outputStream.WriteLine("<a href=/test>return</a><p>");
        p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data);
    }
}

四 添加一個安裝程序類

技術分享圖片

大概步驟已經完成了剩下就是打包發布 源碼下載鏈接 https://files.cnblogs.com/files/pengdakun/MyFirstWindowsService.rar

打包好程序下載鏈接 https://files.cnblogs.com/files/pengdakun/%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.rar

學習記錄---C# Web程序獲取客戶端電腦信息