Esempio n. 1
0
        static string v8_get_raw(object p)
        {
            if (p == null || string.IsNullOrWhiteSpace(p.ToString()))
            {
                return(string.Empty);
            }
            string url = p.ToString();

            V8ScriptEngine engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging, 9393);

            //engine.Execute(Script_Text);
            //engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
            //object returnedVal = _v8Engine.Script.Execute();

            engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
            engine.Execute(@"
    function get(url) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, false);
        xhr.send();

        if (xhr.status == 200) return xhr.responseText;
        else return 'ERR: Request failed: ' + xhr.status;
    }
");

            string v = engine.Script.get(url);

            return(v);
        }
        public void V8ScriptEngine_AddCOMType_XMLHTTP()
        {
            var    status = 0;
            string data   = null;

            var thread = new Thread(() =>
            {
                using (var testEngine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging))
                {
                    testEngine.Script.onComplete = new Action <int, string>((xhrStatus, xhrData) =>
                    {
                        status = xhrStatus;
                        data   = xhrData;
                        Dispatcher.ExitAllFrames();
                    });

                    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        // ReSharper disable AccessToDisposedClosure

                        testEngine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
                        testEngine.Execute(@"
                            xhr = new XMLHttpRequest();
                            xhr.open('POST', 'http://httpbin.org/post', true);
                            xhr.onreadystatechange = function () {
                                if (xhr.readyState == 4) {
                                    onComplete(xhr.status, JSON.parse(xhr.responseText).data);
                                }
                            };
                            xhr.send('Hello, world!');
                        ");

                        // ReSharper restore AccessToDisposedClosure
                    }));

                    Dispatcher.Run();
                }
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            if (!thread.Join(TimeSpan.FromSeconds(5)))
            {
                Assert.Inconclusive("The Httpbin service request timed out");
            }

            Assert.AreEqual(200, status);
            Assert.AreEqual("Hello, world!", data);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            string s = string.Empty;

            try
            {
                string url = "https://zingnews.vn";
                if (args.Length > 0)
                {
                    url = args[0];
                }

                //V8ScriptEngine engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
                V8ScriptEngine engine = new V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers);
                //engine.Execute(Script_Text);
                engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
                //object returnedVal = _v8Engine.Script.Execute();
                //return returnedVal;
                //engine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP");
                engine.Execute(@"
    function get(url) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', url, false);
        xhr.send();
        if (xhr.status == 200)
            return xhr.responseText;
        return '';
    }
");

                s = engine.Script.get(url);
                engine.Dispose();
            }
            catch (Exception e)
            {
                s = "ERROR: " + e.Message;
            }

            Console.OutputEncoding = System.Text.Encoding.UTF8;

            Console.WriteLine(s);

            //Force garbage collection.
            GC.Collect();
            // Wait for all finalizers to complete before continuing.
            GC.WaitForPendingFinalizers();
        }