HttpWebResponse 不包含 cookie
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能适用于这里,我在会话方面遇到过这个问题,所以我假设 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.