使用FiddlerCore来截取替换Http请求中的网页内容
做过测试的应该都知道Fiddler,它可以很方便截取Internet上的网页替换成本地的,或者修改其中的一部分内容后呈现。简单地说就是可能监测所有HTTP连接,设置断点,胡乱修改。是测试调试的一件利器。
使用Fiddler的开放组件,我们可以将其集成到自己的程序中,如生成flash/silverlight所需要的crossdomain.xml,clientaccesspolicy.xml安全文件等:
Fiddler的API: http://www.fiddler2.com/fiddler/dev/ScriptSamples.asp
下面是一个小例子:自动生成所有的silverlight安全策略文件:
using System;
using Fiddler;
namespace AccessPolicyTool
{
class Program
{
const string PolicyXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers=""*"">
<domain uri=""http://*""/>
</allow-from>
<grant-to>
<resource path=""/"" include-subpaths=""true""/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>";
//find and replace the client access policy file.
static void Main(string[] args)
{
//List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();
Fiddler.FiddlerApplication.BeforeRequest += new SessionStateHandler(FiddlerApplication_BeforeRequest);
Fiddler.FiddlerApplication.BeforeResponse += new Fiddler.SessionStateHandler(FiddlerApplication_BeforeResponse);
Fiddler.FiddlerApplication.Startup(8877, FiddlerCoreStartupFlags.Default);
Console.ReadKey();
Fiddler.FiddlerApplication.Shutdown();
}
static void FiddlerApplication_BeforeRequest(Session oSession)
{
Console.WriteLine("FiddlerApplication_BeforeRequest");
if (oSession.fullUrl.IndexOf("clientaccesspolicy.xml") > 0)
{
oSession.bBufferResponse = true;
}
}
//find and replace the client access policy file.
static void FiddlerApplication_BeforeResponse(Fiddler.Session oSession)
{
if (oSession.fullUrl.IndexOf("clientaccesspolicy.xml") > 0)
{
Console.WriteLine(oSession.fullUrl);
oSession.utilDecodeResponse();
oSession.utilSetResponseBody(PolicyXml);
oSession.oResponse.headers.HTTPResponseCode = 200;
oSession.oResponse.headers.HTTPResponseStatus = "200 OK";
oSession.oResponse.headers["Content-Type"] = "text/xml";
oSession.oResponse.headers.Remove("WWW-Authenticate");
}
}
}
}
FiddlerCore 修改HTTP返回结果
发了封邮件给官网,问题解决了。
在BeforeRequest事件中设置Session.bBufferResponse = true 就可以了。
使用FiddlerCore来截取替换Http请求中的网页内容的更多相关文章
- asp.net 字符串替换、截取和从字符串中最后某个字符 开始截取
有时候要在一段字符串里面把某些字符替换成其他字符,怎么办? 例如: string image=@"csks/news/user_top/qqqq/qqqq.jpg"; image ...
- 【原创】http请求中加号被替换为空格?源码背后的秘密
这是why技术的第**20**篇原创文章 ) and ifdown() auto eth0 iface eth0 inet static address 2 ...
- Java邻接表表示加权有向图,附dijkstra最短路径算法
从A到B,有多条路线,要找出最短路线,应该用哪种数据结构来存储这些数据. 这不是显然的考查图论的相关知识了么, 1.图的两种表示方式: 邻接矩阵:二维数组搞定. 邻接表:Map<Vertext, ...
- X-Forwarded-For 会少记录一次代理服务器的IP
X-Forwarded-For是一个Http请求头中的项目. 当一个请求经过代理时,X-Forwarded-For将被记录下来,规则如下: 假设用户U,通过代理服务器A,请求到服务器S, 那么X-Fo ...
- 初始化ArrayList的两种方法[转]
方式一: ArrayList<String> list = new ArrayList<String>(); String str01 = String("str ...
- 【TP3.2+Oracle】数据进行分页
1.写在前面:mysql的分页 通过limit 关键字进行处理, oracle却没有limit,而是用ROWNUM 字段来进行分页 2.参考示例,TP3.2 代码,其实原理看懂了 其他框架和原生都可以 ...
- 利用Jquery和fullCalendar制作日程表
详情请参考以下代码 <!DOCTYPE html> <html> <head> <title>利用Jquery和fullCalendar制作日程表< ...
- 【RS】Local Low-Rank Matrix Approximation - LLORMA :局部低秩矩阵近似
[论文标题]Local Low-Rank Matrix Approximation (icml_2013 ) [论文作者]Joonseok Lee,Seungyeon Kim,Guy Lebanon ...
- 重写Checkbox 改写选择框的大小
/* 作者:Starts_2000 * 日期:2009-07-30 * 网站:http://www.csharpwin.com CS 程序员之窗. * 你可以免费使用或修改以下代码,但请保留版权信息. ...