toy*_*fun 4 c# proxy fiddler fiddlercore
我想建立一个代理服务器来处理本地请求.
例如 - 捕获Web浏览器打印请求并将它们发送到适当的LAN打印机.
到目前为止,我的想法是在Windows服务中托管FiddlerCore引擎.
这是我的代码:
private static void Main()
{
FiddlerApplication.OnNotification += (sender, e) => Console.WriteLine("** NotifyUser: " + e.NotifyString);
FiddlerApplication.Log.OnLogString += (sender, e) => Console.WriteLine("** LogString: " + e.LogString);
FiddlerApplication.BeforeRequest += oSession => { Console.WriteLine("Before request for:\t" + oSession.fullUrl); oSession.bBufferResponse = true; ParseRequest(oSession); };
FiddlerApplication.BeforeResponse += oSession => Console.WriteLine("{0}:HTTP {1} for {2}", oSession.id, oSession.responseCode, oSession.fullUrl);
FiddlerApplication.AfterSessionComplete += oSession => Console.WriteLine("Finished session:\t" + oSession.fullUrl);
Console.CancelKeyPress += Console_CancelKeyPress;
Console.WriteLine("Starting FiddlerCore...");
CONFIG.IgnoreServerCertErrors = true;
FiddlerApplication.Startup(8877, true, true);
Console.WriteLine("Hit CTRL+C to end session.");
Object forever = new Object();
lock (forever)
{
Monitor.Wait(forever);
}
}
private static void ParseRequest(Session oSession)
{
switch (oSession.host)
{
case "localhost.":
Console.WriteLine("Handling local request...");
break;
}
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("Shutting down...");
FiddlerApplication.Shutdown();
Thread.Sleep(750);
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
该ParseRequest函数可以识别本地请求(即http://localhost./print?whatever),但是如何阻止代理向前转发它们(解析并执行打印请求但没有得到404页面)?
自己找到解决方案:
在BeforeRequest回调中,Fiddler.Session对象作为参数传递.
调用其utilCreateResponseAndBypassServer功能时,请求不会中继到任何服务器,而是在本地处理.
所以,我的新ParseRequest功能如下:
private static void ParseRequest(Session oSession)
{
if (oSession.hostname != "localhost") return;
Console.WriteLine("Handling local request...");
oSession.bBufferResponse = true;
oSession.utilCreateResponseAndBypassServer();
oSession.oResponse.headers.HTTPResponseStatus = "200 Ok";
oSession.oResponse["Content-Type"] = "text/html; charset=UTF-8";
oSession.oResponse["Cache-Control"] = "private, max-age=0";
oSession.utilSetResponseBody("<html><body>Handling local request...</body></html>");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1776 次 |
| 最近记录: |