在.NET中以编程方式监听HTTP流量有什么办法吗?

时间:2022-07-15 19:41:43

I'm using browser automation for testing web sites but I need to verify HTTP requests from the browser (i.e., images, external scripts, XmlHttpRequest objects). Is there a way to programmatically instantiate a proxy for the browser to use in order to see what its sending?

我正在使用浏览器自动化来测试网站,但我需要验证来自浏览器的HTTP请求(即图像,外部脚本,XmlHttpRequest对象)。有没有办法以编程方式实例化浏览器使用的代理,以便查看它的发送内容?

I'm already using Fiddler to watch the traffic but I want something that's UI-less that I can use in continuous build integration.

我已经在使用Fiddler来观察流量,但我想要的东西是无需UI的,我可以在连续构建集成中使用。

1 个解决方案

#1


3  

I have briefly looked into the same thing and have considered two solutions (but haven't tried them yet).

我简要地研究了同样的事情,并考虑了两种解决方案(但尚未尝试过)。

The first suggestion I have would be to use the HttpListener class (and possibly Webclient or other System.Net http related classes to re-post the requests to your application) as a proxy for the WebBrowser test calls. I have no experience with HttpListener, but it looks like a simple and promising way to proxy calls through to your app.

我的第一个建议是使用HttpListener类(可能还有Webclient或其他System.Net http相关类将请求重新发布到您的应用程序)作为WebBrowser测试调用的代理。我没有使用HttpListener的经验,但它看起来像是一种简单而有前途的方式来将调用代理到您的应用程序。

The second suggestion I have is to do what Fiddler does: tap into the WinINET Http stack on your test machine to act as a proxy (with some sort of filter to narrow it down to JUST your WebBrowser's calls). Unforunately, the best example of this I have been able to find is Fiddler itself and the only way I know to get the code is Reflector. It might be possible to get to the Fiddler proxy code another way, but I did not have time to investigate this path fully.

我的第二个建议就是做Fiddler所做的事情:点击测试机器上的WinINET Http堆栈作为代理(使用某种过滤器将其缩小到只有你的WebBrowser的调用)。不幸的是,我能找到的最好的例子是Fiddler本身,我知道获取代码的唯一方法是Reflector。有可能以另一种方式获取Fiddler代理代码,但我没有时间完全调查此路径。

HTH!, Richard

#1


3  

I have briefly looked into the same thing and have considered two solutions (but haven't tried them yet).

我简要地研究了同样的事情,并考虑了两种解决方案(但尚未尝试过)。

The first suggestion I have would be to use the HttpListener class (and possibly Webclient or other System.Net http related classes to re-post the requests to your application) as a proxy for the WebBrowser test calls. I have no experience with HttpListener, but it looks like a simple and promising way to proxy calls through to your app.

我的第一个建议是使用HttpListener类(可能还有Webclient或其他System.Net http相关类将请求重新发布到您的应用程序)作为WebBrowser测试调用的代理。我没有使用HttpListener的经验,但它看起来像是一种简单而有前途的方式来将调用代理到您的应用程序。

The second suggestion I have is to do what Fiddler does: tap into the WinINET Http stack on your test machine to act as a proxy (with some sort of filter to narrow it down to JUST your WebBrowser's calls). Unforunately, the best example of this I have been able to find is Fiddler itself and the only way I know to get the code is Reflector. It might be possible to get to the Fiddler proxy code another way, but I did not have time to investigate this path fully.

我的第二个建议就是做Fiddler所做的事情:点击测试机器上的WinINET Http堆栈作为代理(使用某种过滤器将其缩小到只有你的WebBrowser的调用)。不幸的是,我能找到的最好的例子是Fiddler本身,我知道获取代码的唯一方法是Reflector。有可能以另一种方式获取Fiddler代理代码,但我没有时间完全调查此路径。

HTH!, Richard