使用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**篇原创文章
一.下载nginx安装包 访问地址:https://nginx.org 根据自己的需求下载合适的安装包 二.安装gcc gcc是用来编译下载下来的nginx源码 yum install gcc-c++ ...
- Linux 防火墙Iptables
1.规则链INPUT——进来的数据包应用此规则链中的策略OUTPUT——外出的数据包应用此规则链中的策略FORWARD——转发数据包时应用此规则链中的策略PREROUTING——对数据包作路由选择前应 ...
- (原)lua及torch中的type
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6591641.html 说明:本文不一定正确... 如果要是variable:type(),则返回tor ...
- SQL之group by 和 having
转自:mysql必知必会——GROUP BY和HAVING GROUP BY语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. select子句中的列名必须为分组列或列函 ...
- <转>字节码指令
本文转自:http://www.cnblogs.com/nazhizq/p/6525263.html 在llimits.h文件中定义了指令的类型.其实就是32个字节. typedef lu_int32 ...
- 超酷的 Vim 搜索技巧
尽管目前我们已经涉及 Vim 的多种特性,但此编辑器的特性集如此庞大,不管我们学习多少,似乎仍然远远不足.承接我们的 Vim 教程系列,本文我们将讨论 Vim 提供的多种搜索技术. 不过在此之前,请注 ...
- linux达人养成计划学习笔记(四)—— 压缩命令
一.常见的压缩格式: 二..zip格式压缩 1.压缩文件.文件夹 zip 压缩后文件名(.zip结尾) 压缩文件名zip -r 压缩后文件夹(.zip结尾) 压缩文件 2.解压缩 unzip 压缩文件 ...
- mac使用phpize进行安装的时候碰到的问题
问题: grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include/php/Zend/zend_mo ...
- easyUI设置textbox的值
我们知道<input type="text" class="easyui-validatebox" id="txtrName" nam ...