使用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请求中的网页内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 毫秒级的时间处理上G的图片(生成缩略图)
- 下一篇: 学习js,尝试写一个表单验证框架(1)-