我用的是小提琴手核心NET庫作爲本地代理來記錄網絡用戶的活動。然而,我最終遇到了一個似乎很難解決的問題。我有一個網頁瀏覽器說谷歌瀏覽器,用戶打開10個不同的標籤,每個標籤有不同的網址。問題是,代理單獨記錄所有通過每個頁面發起HTTP會話,害我找出使用我的智慧,其相應的HTTP會話屬於標籤。我明白這是因爲HTTP協議的無狀態本質。不過,我只是想知道有沒有簡單的方法來做到這一點?我結束了在提琴手中的C#代碼。由於啓發式方法,它仍然不是一個可靠的解決方案。如何可靠地將代理中的HTTP會話分類到相應的瀏覽器用戶正在查看的窗口/標籤?
這是對與.NET 4的Fiddler core捆綁在一起的示例項目的修改。基本上它是過濾在最近幾秒內啓動的HTTP會話以查找第一個請求或切換到由同一選項卡創建的另一個頁面瀏覽器。它幾乎可行,但似乎不是一個通用的解決方案。
Fiddler.FiddlerApplication.AfterSessionComplete += delegate(Fiddler.Session oS)
{
//exclude other HTTP methods
if (oS.oRequest.headers.HTTPMethod == "GET" || oS.oRequest.headers.HTTPMethod == "POST")
//exclude other HTTP Status codes
if (oS.oResponse.headers.HTTPResponseStatus == "200 OK" || oS.oResponse.headers.HTTPResponseStatus == "304 Not Modified")
{
//exclude other MIME responses (allow only text/html)
var accept = oS.oRequest.headers.FindAll("Accept");
if (accept != null)
{
if(accept.Count>0)
if (accept[0].Value.Contains("text/html"))
{
//exclude AJAX
if (!oS.oRequest.headers.Exists("X-Requested-With"))
{
//find the referer for this request
var referer = oS.oRequest.headers.FindAll("Referer");
//if no referer then assume this as a new request and display the same
if(referer!=null)
{
//if no referer then assume this as a new request and display the same
if (referer.Count > 0)
{
//lock the sessions
Monitor.Enter(oAllSessions);
//filter further using the response
if (oS.oResponse.MIMEType == string.Empty || oS.oResponse.MIMEType == "text/html")
//get all previous sessions with the same process ID this session request
if(oAllSessions.FindAll(a=>a.LocalProcessID == oS.LocalProcessID)
//get all previous sessions within last second (assuming the new tab opened initiated multiple sessions other than parent)
.FindAll(z => (z.Timers.ClientBeginRequest > oS.Timers.ClientBeginRequest.AddSeconds(-1)))
//get all previous sessions that belongs to the same port of the current session
.FindAll(b=>b.port == oS.port).FindAll(c=>c.clientIP ==oS.clientIP)
//get all previus sessions with the same referrer URL of the current session
.FindAll(y => referer[0].Value.Equals(y.fullUrl))
//get all previous sessions with the same host name of the current session
.FindAll(m=>m.hostname==oS.hostname).Count==0) //if count ==0 that means this is the parent request
Console.WriteLine(oS.fullUrl);
//unlock sessions
Monitor.Exit(oAllSessions);
}
else
Console.WriteLine(oS.fullUrl);
}
else
Console.WriteLine(oS.fullUrl);
Console.WriteLine();
}
}
}
}
};
因此沒有可靠的方法。我想我會使用一些啓發式來提高準確性。等待任何其他更好的答案,如果不是,我會將此標記爲已接受。謝謝。 – justcoding124