1.Engine var engine = new Engine();
string file = Server.MapPath("../Script/challenge_6.js"); StreamReader streamReader = new StreamReader(file); string script = streamReader.ReadToEnd(); streamReader.Close(); var fromValue = engine.Execute(script).GetValue("getHexinV"); //getHexinV 为方法名
var nn = fromValue.Invoke();//执行方法
2.node.js
1.本地安装node.js
2.nuget安装 Microsoft.AspNetCore.NodeServices
3.注册服务
services.AddNodeServices();
4.使用
[HttpPost]
public async Task<string> Test([FromServices] INodeServices nodeServices)
{
var lineResult = await nodeServices.InvokeAsync<string>(@"E:\学习项目\WebCore\WebCore\Script\challenge_6.js", "晓晨");
return "";
}
注意js必须包含回调函数,才能使用
module.exports = function (callback, name) { var greet = function (name) { return "Hello " + name; } callback(null, greet(name)); }
3.使用无界面浏览器控件 IWebDriver (目前只发现window可以正常使用)
参考:https://www.cnblogs.com/endlock/p/6423613.html
1. 搜索Selenium,安装Selenium.WebDriver。注意:如果要使用代理的话最好安装3.0.0版本。
2.安装运行环境 Selenium.PhantomJS.WebDriver
3.使用
using OpenQA.Selenium; using OpenQA.Selenium.PhantomJS; using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var url = "http://www.baidu.com"; IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService()); driver.Navigate().GoToUrl(url); Console.WriteLine(driver.PageSource); Console.Read(); } private static PhantomJSDriverService GetPhantomJSDriverService() { PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService(); //设置代理服务器地址 //pds.Proxy = $"{ip}:{port}"; //设置代理服务器认证信息 //pds.ProxyAuthentication = GetProxyAuthorization(); return pds; } } }