将Windows窗体应用程序转换为Asp lr控制台应用程序

时间:2017-02-14 05:11:10

标签: c# dotnetbrowser

我正在使用vs 2017rc,我有兼容性问题。我无法在我的项目中添加windows形式玩偶,当我尝试将代码从win形式转换为Asp时,会出现问题。也许我做错了,但它似乎适用于vs2015。

我需要帮助来解决这个问题。也许我做错了。请参阅下面的代码。

using DotNetBrowser;
using DotNetBrowser.WinForms;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

namespace GetAjaxResponseBodySample
{
public partial class Form1 : Form
{
    private static List<string> ajaxUrls = new List<string>();
    private WinFormsBrowserView browserView;

    public Form1()
    {
        InitializeComponent();
        browserView = new WinFormsBrowserView();
        browserView.Browser.Context.NetworkService.ResourceHandler = new AjaxResourceHandler();
        browserView.Browser.Context.NetworkService.NetworkDelegate = new AjaxNetworkDelegate();
        Controls.Add(browserView);
        browserView.Browser.LoadURL("http://www.w3schools.com/xml/ajax_examples.asp");
    }

    private class AjaxResourceHandler : ResourceHandler
    {
        public bool CanLoadResource(ResourceParams parameters)
        {
            if (parameters.ResourceType == ResourceType.XHR)
            {
                Debug.WriteLine("Intercepted AJAX request: " + parameters.URL);
                ajaxUrls.Add(parameters.URL);
            }
            return true;
        }
    }

    private class AjaxNetworkDelegate : DefaultNetworkDelegate
    {
        public override void OnDataReceived(DataReceivedParams parameters)
        {
            if (ajaxUrls.Contains(parameters.Url))
            {
                Debug.WriteLine("Captured response for: " + parameters.Url);
                Debug.WriteLine("MimeType = " + parameters.MimeType);
                Debug.WriteLine("Charset = " + parameters.Charset);
                PrintResponseData(parameters.Data);
            }
        }

        private void PrintResponseData(byte[] data) {
            Debug.WriteLine("Data = ");
            var str = Encoding.Default.GetString(data);
            Debug.WriteLine(str);
        }
    }
}

我不关心浏览器视图......我已经从Ajax响应体中获得了我需要的Jason。

1 个答案:

答案 0 :(得分:3)

可以在无头模式下使用Browser而无需创建BrowserView。

以下示例代码在VS2017rc上的Web应用程序中使用ASP.NET Core Web Application(.NET Framework)或ASP.NET Web应用程序(.NET Framework)。 请注意在捕获回复正文后必须Dispose browser

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using DotNetBrowser;
using System.Diagnostics;
using System.Text;

namespace WebApplication7.Controllers
{
    public class HomeController : Controller
    {
        private static List<string> ajaxUrls = new List<string>();
        Browser browser;        

        public string Index()
        {
            Init();
            return "Test page";
        }        

        private void Init()
        {
            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");
        }       

        private class AjaxResourceHandler : ResourceHandler
        {
            public bool CanLoadResource(ResourceParams parameters)
            {
                if (parameters.ResourceType == ResourceType.XHR)
                {
                    Debug.WriteLine("Intercepted AJAX request: " + parameters.URL);
                    ajaxUrls.Add(parameters.URL);
                }
                return true;
            }
        }

        private class AjaxNetworkDelegate : DefaultNetworkDelegate
        {
            public override void OnDataReceived(DataReceivedParams parameters)
            {
                if (ajaxUrls.Contains(parameters.Url))
                {
                    Debug.WriteLine("Captured response for: " + parameters.Url);
                    Debug.WriteLine("MimeType = " + parameters.MimeType);
                    Debug.WriteLine("Charset = " + parameters.Charset);
                    PrintResponseData(parameters.Data);
                }
            }

            private void PrintResponseData(byte[] data)
            {
                Debug.WriteLine("Data = ");
                var str = Encoding.UTF8.GetString(data);
                Debug.WriteLine(str);
            }
        }
    }
}