并非所有 cookie 都使用 cookiecontainer 和 webclient/httpwebrequest 保存
我正在尝试使用 CookieAwareClient 登录网站(此处代码:C# WebClient 登录网站)。当我使用网络浏览器登录该网站时,我会收到大约 10 个 cookie。如果我禁用 JavaScript 并尝试登录,我会得到 5(activeTab、id、mcim、PHPSESSID、用户名)。但是当我尝试使用 CookieAwareClient 登录时,唯一保存的 cookie 是 PHPSESSID。
其他 cookie 未保存的原因可能是什么?我知道 WebClient 不会执行 JavaScript,但禁用 JavaScript 时生成的其他 3 个 cookie 也不会被保留。
I am trying to log in to a site using CookieAwareClient (code here: C# WebClient Log onto Website). When I log in to the site using my web browser, I get about 10 cookies. If I disable JavaScript and try to log in, I get 5 (activeTab, id, mcim, PHPSESSID, username). But when I try to log in using the CookieAwareClient the only cookie being saved is PHPSESSID.
What could be the reason the that the other cookies are not being saved? I know the WebClient does not execute JavaScript but the 3 other cookies that are generated when JavaScript is disabled are not being preserved either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,一个明显的问题:在您关闭浏览器中的脚本并尝试登录之前,您是否先清除了cookie?如果没有,请清除 cookie,然后重试。
假设您首先清除了 cookie,并且在无脚本登录后仍然获得了 5 个新 cookie,那么通过查看丢失的 cookie 名称,我猜测您的 CookieAwareClient 实际上并未成功登录用户。如果登录不成功,我期望缺少“用户名”cookie。当您查看登录请求返回的 HTML 时,看起来是否登录成功?或者它返回某种错误?
如果返回的 HTML 是成功登录,那么我要考虑的下一个可能的罪魁祸首将是浏览器执行但代码未执行的其他请求。例如,登录页面可以有一个 META REFRESH,它重定向到另一个页面(设置 cookie)。或者登录页面可以包含
IFRAME
、IMG
、SCRIPT
等,从而设置 cookie。要诊断此问题,请使用 Fiddler、Firebug 或其他类似工具来查看浏览器在登录过程中执行了哪些特定 HTTP 请求。确保登录页面的 POST 正在执行所有 cookie 设置,如果没有,那么您需要在初始登录后向代码添加其他请求。如果事实证明只有一个 HTTP 请求设置 cookie,那么问题很可能出在您的代码正在发送(或未发送)到服务器的 HTTP 标头或 POST 数据。浏览器可能会发送不同的标头或 POST 数据。诊断此问题还需要 Firebug、Fiddler 或类似工具来比较浏览器发送的 HTTP 标头和 POST 数据与代码发送的内容。
如果这些建议都不起作用,请发表评论,我们可以迭代。
First, an obvious question: before you turned off script in your browser and tried to log in, did you clear your cookies first? If not, clear cookies and try again.
Assuming you did clear cookies first and still got 5 new cookies after a no-script login, then by looking at the missing cookie names I'd guess that your CookieAwareClient is not actually successfully logging in the user. Lack of a "username" cookie is what I'd expect if the login didn't succeed. When you look at the HTML returned by your login request, does it look like a successful login? Or is it returning some sort of error?
If the HTML returned is a successful login, then the next possible culprit I'd look at would be additional requests being executed by the browser but not executed by your code. For example, the login page could have a META REFRESH which redirected to another page (which set a cookie). Or the login page could contain an
IFRAME
,IMG
,SCRIPT
, etc. which in turn set a cookie. To diagnose this, use Fiddler, Firebug, or other similar tool to see what specific HTTP requests are being executed by your browser as part of the login process. Make sure that the POST to the login page is doing all the cookie-setting, and if not, then you'll need to add additional requests to your code after the initial login.If it turns out there's only one HTTP request setting cookies, then the problem is likley to be the HTTP headers or POST data that your code is sending (or not sending) to the server. The browser is likely sending different headers or POST data. Diagnosing this also requires Firebug, Fiddler, or similar tool to compare the HTTP headers and POST data that a browser is sending vs. what your code is sending.
If none of these suggestions work, post a comment and we can iterate.