HttpWebResponse 不包含 cookie

发布于 2024-10-16 09:20:35 字数 953 浏览 4 评论 0原文

我正在使用 HTTPWebRequest/HTTPWebResponse 与网站 (www.lockerz.com) 进行交互。所以,我在网站上进行身份验证:

HttpWebRequest webRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.lockerz.com/auth/login");
byte[] bytes = Encoding.ASCII.GetBytes("handle=" + textBoxEmail.Text + "&password=" + textBoxPassword.Text);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);

然后我得到响应:

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

我正在使用 Charles 观看请求\响应,它说响应中必须有 cookie,但是当我尝试制作 cookie 列表时,我得到了它说没有饼干!

foreach(Cookie c in webResponse.Cookies) {
    writeToLog("Cookie Name: " + c.Name + " Value: " + c.Value);
    cc.Add(c);
}

我尝试过添加 CookieContainer 但无论如何它都不起作用。

I'm using HTTPWebRequest/HTTPWebResponse to interact with the site (www.lockerz.com). So, I authenticate on the site:

HttpWebRequest webRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.lockerz.com/auth/login");
byte[] bytes = Encoding.ASCII.GetBytes("handle=" + textBoxEmail.Text + "&password=" + textBoxPassword.Text);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
Stream os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);

Then I get the response:

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

I'm watching the requests\responses using Charles and it says that in the response there must be cookies, but when I try to make a list of the cookies I get it says that there are no cookies!

foreach(Cookie c in webResponse.Cookies) {
    writeToLog("Cookie Name: " + c.Name + " Value: " + c.Value);
    cc.Add(c);
}

I've tried adding a CookieContainer but it doesn't work anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-10-23 09:20:35

这可能适用于这里,我在会话方面遇到过这个问题,所以我假设 cookie 也是如此。

您正在拨打电话:http://www.lockerz.com/auth/login。但是如果一个人访问一个没有“www”的页面。 url 的一部分,那么他们的 cookie 将无法在发送到“www”时幸存下来。站点,因为它们是不同的站点(就 cookie 而言)。

我只会向“/auth/login”发出请求,这样,用户浏览器中的 url 是什么就无关紧要了。

希望这有效。

This may apply here, I have had this problem with sessions, so I will assume the same for cookies.

You are making a call to: http://www.lockerz.com/auth/login. But if a person goes to a page without the "www." part of the url, then their cookies won't survive being sent to the "www." site, because they are different sites (as far as the cookies are concerned).

I would just make the request to "/auth/login", that way, it won't matter what the user has as the url in their browser.

Hope this works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文