1
我試圖用FiddlerCore監控流量和web瀏覽器控制器的請求,我有下面的代碼以捕捉C#Web請求捕獲所有FiddlerCore
private void button1_Click(object sender, EventArgs e)
{
List<Fiddler.Session> oAllSessions = new List<Fiddler.Session>();
URLMonInterop.SetProxyInProcess("127.0.0.1:8888", "<-loopback>");
webBrowser1.ScriptErrorsSuppressed = true;
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://localhost:8888");
myProxy.Address = newUri;
Fiddler.FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default);
Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS)
{
Monitor.Enter(oAllSessions);
oAllSessions.Add(oS);
Monitor.Exit(oAllSessions);
};
webBrowser1.Navigate("http://www.test.com/");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
var message = string.Join(Environment.NewLine, oAllSessions);
textBox1.Text = textBox1.Text + message;
Fiddler.FiddlerApplication.Shutdown();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Fiddler.FiddlerApplication.Shutdown();
URLMonInterop.ResetProxyInProcessToDefault();
}
它只返回在webBroser只有一個請求響應(指定網址.Navigate),我看不到在示例站點上對圖像,css和其他加載文件的請求。我找不到任何這信息,可有人請幫我我如何能捕捉到所有的GET POST請求時webBroswer.Navigate給定的URL明白了嗎?
更新:
delegate void updateUI();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Fiddler.FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
Fiddler.FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default);
webBrowser1.ScriptErrorsSuppressed = true;
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://localhost:8888");
myProxy.Address = newUri;
string[] urls = new string[] { "http://localhost/test/page1",
"http://localhost/test/page2 "
};
foreach (string url in urls)
{
webBrowser1.Navigate(url);
// Capture root url
listBox1.Invoke(new updateUI(() =>
{
listBox1.Items.Add(url);
}));
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
// Hack as I am not sure what to do here so wait 10 second for webBrowser to load all requests otherwise I only get last url data in listbox
for (int i = 0; i <= 10; i++)
{
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(1000);
}
}
}
void FiddlerApplication_AfterSessionComplete(Session oSession)
{
var regex = new Regex("keywords-in-url-to-match");
// If my desired keyword match then grab request POST body
if (regex.IsMatch(oSession.fullUrl.ToString()))
{
string requestBody = oSession.GetRequestBodyAsString();
// Capture url and request body. This url is not root url
listBox1.Invoke(new updateUI(() =>
{
listBox1.Items.Add(oSession.fullUrl);
listBox1.Items.Add(System.Web.HttpUtility.UrlDecode(requestBody));
}));
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Fiddler.FiddlerApplication.Shutdown();
}
感謝答覆先生,我其實是讀你的博客文章,最後我想通了這個問題。我忘了設置一個計時器來更新請求會話。 – user969068
換句話說,這裏真正的問題是,你想更新從後臺線程的用戶界面。您需要使用'BeginInvoke'來將您的UI更新從後臺線程封送到UI線程。 – EricLaw
非常感謝,我遵循了您的建議,並提出了上述更新的代碼。我對Csharp非常新手,但努力學習更多。再次感謝這樣一個美妙的FiddlerCore庫。 – user969068