I have gone through the document on the dotnetbrowser website... while I saw an example that shows how to intersect Ajax calls or filter Ajax calls... I want to know if its possible to obtain Ajax request body response after execution. if it is possible how can I go about it?
Asked
Active
Viewed 551 times
1 Answers
1
Yes, it is possible to intercept the responses for the AJAX requests in DotNetBrowser. To implement this, you can use the ResourceHandler
to capture AJAX request URLs and the NetworkDelegate
to intercept and filter responses.
The following sample code demonstrates the possible approach:
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);
}
}
}
}

Vladyslav Tsvek
- 244
- 1
- 10
-
hello, I can't thank you well enough... your code is exactly what I needed. are you part of TeamDev? thanks very much. – Cody Feb 09 '17 at 18:21