通过 dotnetbrowser 归还书籍 json

Returning Books json through dotnetbrowser

我正在尝试 return JSON 字符串通过 XMlHTTPRequest 响应正文。问题是在方法创建和 AjaxResourceHandler 实例之后,它将继续执行 Get 方法,然后再获取 return 空字符串所需的数据。我想要的是能够 return 将响应主体作为 json 字符串。 还有一件事情。网络浏览器可以做同样的事情吗?如果是这样怎么办?

using AjaxRequest.Models;
using DotNetBrowser;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;

namespace AjaxRequest.Controllers
{
public class ValuesController : ApiController
{


    private static List<string> ajaxUrls = new List<string>();
    private static List<string> sourceDat = new List<string>();
    private static Browser browser;

    public ValuesController()
    {
        browser = BrowserFactory.Create();
        browser.Context.NetworkService.ResourceHandler = new AjaxResourceHandler();
        browser.Context.NetworkService.NetworkDelegate = new AjaxNetworkDelegate();

        browser.LoadURL("https://www.w3schools.com/xml/ajax_examples.asp");
        //browser.Dispose();
    }


    // GET api/values
    public IEnumerable<string> Get()
    {
        //Init();
        return sourceDat;

    }

    // GET api/values/5
    public string Get(int id)
    {

        return "value";

    }

    // POST api/values
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }

    //private  void Init()
    //{

    //}

    public class AjaxResourceHandler : ResourceHandler
    {
        public bool CanLoadResource(ResourceParams parameters)
        {
            if (parameters.ResourceType == ResourceType.XHR && parameters.URL.Contains("https://123movies.is/ajax/v2_get_sources"))
            {

                ajaxUrls.Add(parameters.URL);

            }
            return true;
        }
    }

    public class AjaxNetworkDelegate : DefaultNetworkDelegate
    {
        public override void OnDataReceived(DataReceivedParams parameters)
        {
            if (ajaxUrls.Contains(parameters.Url))
            {

                PrintResponseData(parameters.Data);

            }

        }
        public void PrintResponseData(byte[] data)
        {

            var str = Encoding.UTF8.GetString(data);

            BookSource _sources = JsonConvert.DeserializeObject<BookSource>(str);
            sourceDat.Add(_sources.ToString());
            browser.Dispose();
            //source.Add(_sources);

            //return source;
        }

    }

}
}

您可以尝试使用 ManualResetEvent 阻塞主线程,直到捕获到 AJAX 响应。 下面的示例代码演示了如何实现这种方法:

using AjaxRequest.Models;
using DotNetBrowser;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Web.Http;

namespace AjaxRequest.Controllers
{
    public class ValuesController : ApiController
    {

        private static ManualResetEvent waitEvent;
        private static List<string> ajaxUrls = new List<string>();
        private static List<string> sourceDat = new List<string>();
        private static Browser browser;

        public ValuesController()
        {
            waitEvent = waitEvent = new ManualResetEvent(false);
            browser = BrowserFactory.Create();
            browser.Context.NetworkService.ResourceHandler = new AjaxResourceHandler();
            browser.Context.NetworkService.NetworkDelegate = new AjaxNetworkDelegate();

            browser.LoadURL("https://www.w3schools.com/xml/ajax_examples.asp");
            //browser.Dispose();
        }

        // GET api/values
        public IEnumerable<string> Get()
        {
            //Init();
            waitEvent.WaitOne();
            return sourceDat;

        }

        // GET api/values/5
        public string Get(int id)
        {

            return "value";

        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }

        //private  void Init()
        //{

        //}

        public class AjaxResourceHandler : ResourceHandler
        {
            public bool CanLoadResource(ResourceParams parameters)
            {
                if (parameters.ResourceType == ResourceType.XHR && parameters.URL.Contains("https://123movies.is/ajax/v2_get_sources"))
                {

                    ajaxUrls.Add(parameters.URL);

                }
                return true;
            }
        }

        public class AjaxNetworkDelegate : DefaultNetworkDelegate
        {
            public override void OnDataReceived(DataReceivedParams parameters)
            {
                if (ajaxUrls.Contains(parameters.Url))
                {

                    PrintResponseData(parameters.Data);

                }

            }
            public void PrintResponseData(byte[] data)
            {

                var str = Encoding.UTF8.GetString(data);

                BookSource _sources = JsonConvert.DeserializeObject<BookSource>(str);
                sourceDat.Add(_sources.ToString());
                waitEvent.Set();
                browser.Dispose();
                //source.Add(_sources);

                //return source;
            }

        }

    }
}

据我所知,无法直接在 WebBrowser 中拦截 AJAX 响应。似乎有一种解决方法可以通过 ObjectForScripting 拦截它:catch ajax response data in web browser control C#