0

我尝试使用此代码从网站获取 cookie

CookieCollection cookies = new CookieCollection();
CookieContainer cooki = new CookieContainer();
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(weburl.Text);
request.CookieContainer = cooki;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader read = new StreamReader(response.GetResponseStream());
request.CookieContainer = cooki;
request.CookieContainer.Add(cookies);
cookies = response.Cookies;

if(response.Cookies != null &&  response.Cookies.Count != 0)
{
    foreach(Cookie c in response.Cookies)
        listBox2.Items.Add(c.ToString());

}
else
        listBox2.Items.Add("No Cookies");

但是,当我为http://www.cloob.com之类的网站尝试此操作时,什么也没有发生。

我在 C# 中通过 webbrowser 尝试了这个站点并尝试了这段代码

string cooki = webbrowser1.Document.Cookie.tostring();

我得到了很多这样的信息:

饼干=LMOT=1343810524;IdM=否;__utma=204761026.19745671.1343776441.1343805546.1343810526.4;__utmz=204761026.1343776441.1.1.utmccn=(直接)|utmcsr=(直接)|utmcmd=(无); CR=%7B%22online_count%22:%5B%7B%22count%22:8059,%22status%22:%22available%22%7D,%7B%22count%22:%221204%22,%22status%22:% 22invisible%22%7D,%7B%22count%22:%2276%22,%22status%22:%22busy%22%7D%5D%7D; m_id=1343781310_7982757; LCCT=1343809271;__utmb=204761026; __utmc=204761026

现在我怎样才能通过 webrequest 得到这个?

4

1 回答 1

0

虽然您的代码有点多余,但我认为它没有任何问题。当我针对http://www.google.com/. http://www.cloob.com可能是在 javascript 中设置 cookie,这可以解释为什么您可以在WebBrowser对象中看到 cookie,但不能使用WebRequest.

于 2012-08-02T23:10:43.777 回答