Intercepting #Ajax Requests in #CEFSharp (Chrome For C#) Network Programming in
Intercepting #Ajax Requests in #CEFSharp (Chrome For C#) Network Programming in
Home
About
The webbrowser control for .NET is a version of IE. That version of IE can be configured
via the registry, but it’s still IE, and Chrome is the daddy of browsers. So, if you want to
embed a Chrome browser in a C# application, then using CEFSharp is the way to go.
Install-Package CefSharp.WinForms
You then will have to change your project to either x86 or x64, since this won’t work as
AnyCPU.
1 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
Cef.Initialize(settings);
var strInitialUrl = “http://www.yourwebsite.com”;
browser = new ChromiumWebBrowser(strInitialUrl)
{
RenderProcessMessageHandler = new RenderHandler()
};
InteractionHandler.browser = browser;
var jsInterface = new jsMapInterface
{
onHookEvent = InteractionHandler.HookEventHandler
};
browser.RegisterJsObject(“jsInterface”, jsInterface);
Controls.Add(browser);
browser.Dock = DockStyle.Fill;
Now, I have introduced a few new classes here, that haven’t been defined yet, but we’ll get
to them
RenderHandler
jsMapInterface
InteractionHandler
So, let’s start with RenderHandler. This will be used to inject Javascript into the page once
it is loaded. The Javascript will call out to jsMapInterface to record the Ajax event.
void IRenderProcessMessageHandler.OnContextCreated(IWebBrowser
browserControl, IBrowser browser, IFrame frame)
{
var strScript = File.ReadAllText(@”inject.js”);
frame.ExecuteJavaScriptAsync(strScript);
}
}
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
ThisToclass does nothing Close and accept
find out more, includingapa
how to control cookies, see here: Cookie Policy
content of Inject.js and put it into the page.
2 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
document.addEventListener(‘DOMContentLoaded’,
function () {
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener(“readystatechange”,
function() {
if (this.readyState === 4) {
window.jsInterface.hook(this.responseText, method, url, async, user, pass,
“Context”);
}
},
false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
});
This file should be set to “Copy Always”. Note the window.jsInterface.hook – this is the
call back to the C# Code.
public void hook(string message, string method, string url, bool async, string
user, string pass, string context) // Must be lowercase!
{
if (onHookEvent != null)
{
onHookEvent(new hookEvent()
{
message = message,
url = url,
async = async,
user = user,
pass = pass,
context = context
});
}
}
3 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
A very important point to note here that the method MUST be lowercase!
class InteractionHandler
{
public static ChromiumWebBrowser browser;
}
}
You can set a breakpoint on the comment above, to log, or otherwise handle your ajax
response that you capture.
Share this:
Twitter Facebook
Like
Be the first to like this.
Record #Mp4 #H264 video Send #APNS #Push Capture a #webcam image
from a webcam in C# (.NET notifications using #RapidAPI using .NET Core and
Core) #OpenCV
With 1 comment
Categories: Uncategorized
Comments (1) Trackbacks (0) Leave a comment Trackback
1.
david
September 5, 2019 at 10:57 pm
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Close and accept
To find out more, including how to control cookies, see here: Cookie Policy
hi
4 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
1. No trackbacks yet.
Leave a Reply
Follow me on Twitter
My Tweets
Archives
January 2021
December 2020
November 2020
October 2020
August 2020
July 2020
June 2020
May 2020
April 2020
March 2020
February 2020
January 2020
December 2019
November 2019
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Close and accept
To find out more, including how to control cookies, see here: Cookie Policy
August 2019
5 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Close and accept
To find out more, including how to control cookies, see here: Cookie Policy
May 2015
6 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
June 2014
May 2014
April 2014
March 2014
February 2014
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
June 2012
May 2012
March 2012
February 2012
January 2012
December 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Close and accept
To find out more, including how to control cookies, see here: Cookie Policy
July 2010
7 of 8 11/01/2021, 06:39
Intercepting #Ajax requests in #CEFSharp (Chrome for C#) | Network P... https://blog.dotnetframework.org/2018/10/26/intercepting-ajax-requests-...
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
July 2009
June 2009
May 2009
April 2009
March 2009
January 2009
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
Like us on Facebook
Like us on Facebook
Top
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
Close and accept
To find out more, including how to control cookies, see here: Cookie Policy
8 of 8 11/01/2021, 06:39