gpt4 book ai didi

c# - 通过 dotnetbrowser 返回 Books json

转载 作者:行者123 更新时间:2023-11-30 23:15:47 25 4
gpt4 key购买 nike

我正在尝试通过 XMlHTTPRequest 响应正文返回 JSON 字符串。问题是在方法创建和 AjaxResourceHandler 实例之后,它将继续执行 Get 方法,然后再获取返回空字符串的所需数据。我想要的是能够将响应主体作为 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#

关于c# - 通过 dotnetbrowser 返回 Books json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com