使用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**篇原创文章 db2示例用户登陆后,使用脚本语句db2 get snapshot for all on dbname>snap.out 也可以使用db2 get snap ...
- Inno Setup入门(三)——指定压缩方式
Setup段中的compression指定了采用的压缩方式,较高的压缩率需要较多的时间或者需要更大的内存空间,可用的值如下: zip zip/1到zip/9 bzip bzip/1 到bzip/9 l ...
- JavaScript的NaN-唯一 一个自己不等于自己的对象!!
JavaScript的NaN为什么不等于NaN 在JS中 Object === Object 感觉没有任何问题 这两个都代表的一个东西 但是如果你试过 NaN === NaN 是返回false为什么呢 ...
- vs code 问题:preLaunchTask“build”已终止,退出代码为 1。解决办法
菜单:任务-配置任务 改为如下: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation ab ...
- IDEA安装使用 VisualVM 及VisualVM 远程监视
1. VisualVM是什么 按照VisualVM官网(http://visualvm.github.io/)上的介绍,VisualVM是一个集成命令行JDK工具和轻量级分析功能的可视化工具.专为开发 ...
- 如何开发一个基于 Docker 的 Python 应用
前言 Python 家族成员繁多,解决五花八门的业务需求.这里将通过 Python 明星项目 IPython Notebook,使其容器化,让大家掌握基础的 Docker 使用方法. IPython ...
- 【总结 】550,535,553 Mail from must equal authorized user— jenkins(hudson) email163邮箱和26邮箱成功配置总结
Failed to send out e-mail com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal author ...
- ipsec在企业网中的应用(IKE野蛮模式)(转)
from:http://lulu1101.blog.51cto.com/4455468/817954 ipsec在企业网中的应用(IKE野蛮模式) 案例: 本实验采用华为三台F100防火墙,和一台s3 ...
- scp的两种方式
如果host A 与 host B建立了信任连接(B有A的public key),那么从A向B传送文件,或者从B上传回文件都可以省略密码.但是前提是命令是在A上执行的. 从A向B拷贝文件 on hos ...