2009-12-28 31 views
1

我花了一些時間研究這個,仍然無法弄清楚。它看起來很簡單,所以我覺得自己像個白癡一樣問這個問題,但經過一段時間的研究後,我似乎無法得到它的訣竅。C#.NET Cookie處理後登錄

我需要以編程方式登錄到此站點:https://wholesale.frontiercoop.com/,存儲來自登錄名的cookie,並在下次登錄時重新提交。它看起來像是一個POST提交(我使用Firefox上的Firebug收集的),所以我想出瞭如何存儲我認爲的cookie。我只是無法弄清楚如何在下次調用網站時提交它,所以它不會自動將我重定向到登錄頁面。這是否僅僅是Web瀏覽器對象調用的一個參數?

感謝您的幫助。

回答

2

在.NET你必須使用的CookieContainer,如:

HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("https://wholesale.frontiercoop.com/"); 
req.Method = "POST"; 
CookieContainer container = new CookieContainer(); 
req.CookieContainer = container; 
// Write the POST data and get the request... 
... 
// ...and once the request is done, the cookie is in 'container'. 
// Then, for subsequent requests you set the CookieContainer of the request to the one above 
otherRequest.CookieContainer = container; 
+0

是否有可能使用WebBrowser對象執行此操作?我正在嘗試將HTML源代碼從網站中提取出來,並且需要使用WB來訪問此網站。感謝您的答案,順便說一句。 – 2010-01-04 19:35:23

1

當您使用WebRequest對象的下一個請求,您需要分配有你返回的cookie在它的CookieContainer。

example顯示如何使用Hotmail,它應該告訴你你需要的大部分。