我需要讀取網頁並將其內容存儲在字符串中以供進一步處理。
聽起來很簡單,但我有Cookie支持的問題。如何將網頁保存爲支持cookie的字符串(httpWebRequest)
打開頁面說我需要瀏覽器支持cookie(或打開)。
我做了嘗試的方法做到這一點通過HttpWebRequest的 - 這通常工作給我,但我是來與那些不幸的餅乾停頓......
任何想法如何使它工作?
這裏是我的方法:
string ReadHtml (string address, string encoding) {
Uri url = new Uri(address);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.KeepAlive = true;
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpWebRequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
// Code Page
Encoding enc = Encoding.GetEncoding(encoding);
// Read content
StreamReader loResponseStream = new StreamReader(webResponse.GetResponseStream(),enc);
string lcHtml = loResponseStream.ReadToEnd();
webResponse.Close();
loResponseStream.Close();
return lcHtml;
}
我不需要閱讀/管理cookies。只是我想讀的網頁需要從我的cookies中啓用顯示。暫時顯示「請在瀏覽器中啓用Cookie」(而不是「正常」內容)。我在找的是告訴那個頁面我很高興'吃'它的餅乾:) – Maciej 2010-04-06 13:22:55