我正在研究一個涉及一些基本網絡爬行的項目。我一直在很成功地使用HttpWebRequest和HttpWebResponse。對於cookie處理,我只有一個CookieContainer,每次都分配給HttpWebRequest.CookieContainer。我每次都會自動獲得新的cookie,不需要額外的處理。直到很久以前,當一個曾經工作的網站突然停止工作時,這一切都運行良好。我相當肯定這是一個與Cookie有關的問題,但是我沒有記錄它從來沒有使用過的cookies,所以我不能100%確定。CookieContainer處理路徑(誰吃了我的cookie?)
我已經成功地模擬了問題,我用下面的代碼中看到它:
CookieContainer cookieJar = new CookieContainer();
Uri uri1 = new Uri("http://www.somedomain.com/some/path/page1.html");
CookieCollection cookies1 = new CookieCollection();
cookies1.Add(new Cookie("NoPathCookie", "Page1Value"));
cookies1.Add(new Cookie("CookieWithPath", "Page1Value", "/some/path/"));
Uri uri2 = new Uri("http://www.somedomain.com/some/path/page2.html");
CookieCollection cookies2 = new CookieCollection();
cookies2.Add(new Cookie("NoPathCookie", "Page2Value"));
cookies2.Add(new Cookie("CookieWithPath", "Page2Value", "/some/path/"));
Uri uri3 = new Uri("http://www.somedomain.com/some/path/page3.html");
// Add the cookies from page1.html
cookieJar.Add(uri1, cookies1);
// Add the cookies from page2.html
cookieJar.Add(uri2, cookies2);
// We should now have 3 cookies
Console.WriteLine(string.Format("CookieJar contains {0} cookies", cookieJar.Count));
Console.WriteLine(string.Format("Cookies to send to page1.html: {0}", cookieJar.GetCookieHeader(uri1)));
Console.WriteLine(string.Format("Cookies to send to page2.html: {0}", cookieJar.GetCookieHeader(uri2)));
Console.WriteLine(string.Format("Cookies to send to page3.html: {0}", cookieJar.GetCookieHeader(uri3)));
這模擬參觀兩頁,這兩個設置兩個餅乾。然後它會檢查哪些cookie將被設置爲三頁中的每一頁。
這兩個cookie中,一個沒有指定路徑而另一個指定了路徑。當沒有指定路徑時,我認爲cookie會被髮回到該域中的任何頁面,但它似乎只被發送回該特定頁面。我現在認爲這是正確的,因爲它是一致的。
對我來說,主要問題是如何處理指定路徑的cookie。當然,如果指定了一個路徑,那麼cookie應該被髮送到該路徑中包含的任何頁面。因此,在上面的代碼中,'CookieWithPath'應該對/ some/path /中的任何頁面有效,其中包括page1.html,page2.html和page3.html。當然,如果你註釋掉兩個'NoPathCookie'實例,那麼'CookieWithPath'會被髮送到所有三個頁面,就像我期望的那樣。然而,如上所述包含'NoPathCookie',那麼'CookieWithPath'只會被髮送到page2.html和page3.html,而不會被髮送到page1.html。
這是爲什麼,它是正確的?
正在搜索此問題我曾經討論過有關CookieContainer中的域處理問題,但尚未找到有關路徑處理的任何討論。
我使用Visual Studio 2005/.NET 2.0