-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathProgram.cs
130 lines (113 loc) · 5.27 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#region Copyright
// Copyright © 2025, TeamDev. All rights reserved.
//
// Redistribution and use in source and/or binary forms, with or without
// modification, must retain the above copyright notice and the following
// disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using DotNetBrowser.Browser;
using DotNetBrowser.Engine;
using DotNetBrowser.Frames;
using DotNetBrowser.Handlers;
using DotNetBrowser.Net.Events;
using DotNetBrowser.Net.Handlers;
namespace AjaxResponseIntercept
{
/// <summary>
/// This example demonstrates how to intercept the response data
/// for the AJAX requests.
/// </summary>
internal class Program
{
private static readonly Dictionary<string, HttpRequest> AjaxRequests =
new Dictionary<string, HttpRequest>();
private static void Main(string[] args)
{
using (IEngine engine = EngineFactory.Create())
{
using (IBrowser browser = engine.CreateBrowser())
{
engine.Profiles.Default.Network.SendUrlRequestHandler =
new Handler<SendUrlRequestParameters,
SendUrlRequestResponse>(OnSendUrlRequest);
engine.Profiles.Default.Network.ResponseBytesReceived += OnResponseBytesReceived;
engine.Profiles.Default.Network.RequestCompleted += OnRequestCompleted;
browser.Navigation
.LoadUrl("https://www.w3schools.com/xml/tryit.asp?filename=tryajax_first")
.Wait();
IFrame demoFrame = browser.AllFrames.FirstOrDefault(FrameHasDemoElement);
if (demoFrame != null)
{
//Click the button in the demo frame to make an AJAX request.
Console.WriteLine("Demo frame found");
demoFrame.Document.GetElementByTagName("button").Click();
}
Console.WriteLine("Wait for 15 seconds to be sure that at least some requests are completed.");
Thread.Sleep(15000);
// The dictionary will contain some requests, including the one we sent by clicking the button.
string key =
AjaxRequests.Keys.FirstOrDefault(k => k.Contains("ajax_info.txt"));
if (!string.IsNullOrEmpty(key))
{
HttpRequest ajaxRequest = AjaxRequests[key];
Console.WriteLine($"Response intercepted: \n{ajaxRequest.Response}");
}
}
}
Console.WriteLine("Press any key to terminate...");
Console.ReadKey();
}
private static bool FrameHasDemoElement(IFrame frame)
=> frame.Document.GetElementById("demo") != null;
private static void OnRequestCompleted(object s, RequestCompletedEventArgs e)
{
//Here, we mark the requests as completed for the previously filtered URLs.
string url = e.UrlRequest.Url;
if (AjaxRequests.ContainsKey(url))
{
AjaxRequests[url].Complete();
}
}
private static void OnResponseBytesReceived(object s, ResponseBytesReceivedEventArgs e)
{
//Here, we collect the response data for the previously filtered URLs.
string url = e.UrlRequest.Url;
if (AjaxRequests.ContainsKey(url))
{
HttpRequest httpRequest = AjaxRequests[url];
if (httpRequest.MimeType == null)
{
httpRequest.MimeType = e.MimeType;
}
httpRequest.AppendResponseBytes(e.Data);
}
}
private static SendUrlRequestResponse OnSendUrlRequest(SendUrlRequestParameters arg)
{
// Here, we check the URL request and decide if we want to intercept a response for it.
// For instance, we can check the resource type. This works for AJAX requests.
// We can also check the arg.UrlRequest.Url to determine if this is a URL we need.
if (arg.UrlRequest.ResourceType == ResourceType.Xhr)
{
AjaxRequests[arg.UrlRequest.Url] = new HttpRequest(arg.UrlRequest.Url, arg.UrlRequest.Method);
}
return SendUrlRequestResponse.Continue();
}
}
}