0

我创建了以下代码,据我所知应该可以正常工作吗?它根本没有收到任何 cookie,我已经与 Wire Shark 进行了仔细检查,并且 cookie 正在被退回……这是在 Windows Phone 7 上开发的。

            byte[] content = GetLoginRequestContent(username, password);

        CookieContainer cookieContainer = new CookieContainer();
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(LoginUri);
        httpWebRequest.ContentType = AuthContentType;
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers["referer"] = LoginRequestReferer;
        httpWebRequest.CookieContainer = cookieContainer;
        httpWebRequest.Headers[HttpRequestHeader.ContentLength] = content.Length.ToString();



        httpWebRequest.BeginGetRequestStream(async1 =>
        {
            using (Stream stream = httpWebRequest.EndGetRequestStream(async1))
                stream.Write(content, 0, content.Length);
            httpWebRequest.BeginGetResponse(async2 =>
            {
                HttpWebResponse rep = (HttpWebResponse)httpWebRequest.EndGetResponse(async2);
                CookieCollection cookies = rep.Cookies;
                using (Stream stream = rep.GetResponseStream())
                using (StreamReader sr = new StreamReader(stream))
                {
                    String contentX = sr.ReadToEnd();
                    //if blah blah
                }
            }, null);
        }, null);
4

1 回答 1

2

如果 cookie 标记为HttpOnly(会话 cookie 经常出现这种情况),出于安全原因,您无法通过客户端脚本访问它们。它被发送到客户端,客户端在后续请求中将其重新发送到服务器(如果它拥有 cookie 容器),但您无法在客户端读取它的值。

于 2012-06-14T06:25:55.397 回答