How can I use HTML5 geolocation in C# application

前端 未结 4 1425
情书的邮戳
情书的邮戳 2021-02-05 08:05

I\'m developing an anti-theft software to get computers exact location. Notebooks with built-in gps are very rare in my country so I have to use HTML5 Geolocation in my applicat

4条回答
  •  心在旅途
    2021-02-05 08:46

    You can embed a webkit browser into your application by using PhantomJS. PhantomJS is a headless browser and can be added to your application through searching NuGet or the NuGet command line PM> Install-Package PhantomJS. Once PhantomJS has been added to your project, you'll want to build a file to control phantom something like:

     public string PhantomJson(string phantomControlFile, params string[] arguments)
            {
                string returnJsonString = String.Empty;
    
                if (!String.IsNullOrEmpty(URL))
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo
                    {
                        CreateNoWindow = true,
                        RedirectStandardError = true,
                        RedirectStandardOutput = true,
                        FileName = Path.Combine(PhantomExecutionPath, "phantomjs.exe"),
                        UseShellExecute = false,
                        WorkingDirectory = PhantomExecutionPath,
                        Arguments = @"--proxy-type=none --ignore-ssl-errors=true {1} ""{0}"" {2}".FormatWith(URL, phantomControlFile, 
                            arguments.Any() ? String.Join(" ", arguments) : String.Empty)
                    };
    
                    StringBuilder receivedData = new StringBuilder();
                    using (Process p = Process.Start(startInfo))
                    {
                        p.OutputDataReceived += (o, e) =>
                        {
                            if (e.Data != null && e.Data != "failed")
                            {
                                //returnJsonString = e.Data;
                                receivedData.AppendLine(e.Data);
                            }
                        };
                        p.BeginOutputReadLine();
                        p.WaitForExit();
                    }
                    returnJsonString = receivedData.ToString();
    
                    if (!String.IsNullOrEmpty(returnJsonString))
                    {
                        return returnJsonString;
                    }
                    else
                    {
                        throw new ArgumentNullException("Value returned null. Unable to retrieve data from server");
                    }
                }
                else
                {
                    throw new ArgumentNullException("Url cannot be null");
                }
            }
    

    Then you'll want to build a control file to tell phantomjs where to go; something like:

    var args, myurl, page, phantomExit, renderPage, system;
    
    system = require("system");
    args = system.args;
    page = null;
    myurl = args[1];
    
    phantomExit = function(exitCode) { // this is needed as there are time out issues when it tries to exit.
      if (page) {
        page.close();
      }
      return setTimeout(function() {
        return phantom.exit(exitCode);
      }, 0);
    };
    
    renderPage = function(url) {
      page = require("webpage").create();
        return page.open(url, function(status) {
          if (status === 'success') {
             // Process Page and console.log out the values
             return phatomExit(0);
          } else {
             console.log("failed");
             return phantomExit(1);
          }
         });
       };
      renderPage(myurl);
    

提交回复
热议问题