C# Web 服务器看不到存储的 cookie
我正在编写一段 C# 代码测试片段,用于登录 Facebook 并捕获 cookie。我这样做只是为了教育目的。我的任务是在我的工作和第三方之间自动执行表单上传、下载和表单提交,这是我必须做对的事情之一,而且我必须第一次就做对;因此,我想对 Facebook 和其他一些网络服务进行一些练习。
我有:
string postMessage = "user=" + username + "&pass=" + password;
CookieContainer cookieJar = new CookieContainer();
MessageBox.Show(cookieJar.Count + " cookies"); // 0 cookies, as expected.
HttpWebRequest wReq
= (HttpWebRequest)HttpWebRequest.Create("https://www.facebook.com/login.php");
wReq.Proxy = HttpWebRequest.GetSystemWebProxy();
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
wReq.UserAgent
= "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 "
+ "(KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24";
wReq.CookieContainer = cookieJar;
wReq.Method = "POST";
wReq.ContentLength = postMessage.Length;
wReq.ContentType = "application/x-www-form-urlencoded";
StreamWriter sw = null;
try
{
sw = new StreamWriter(wReq.GetRequestStream());
sw.Write(postMessage);
}
catch
{
return false;
}
finally
{
sw.Close();
}
string result = string.Empty;
HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse();
using (StreamReader sr = new StreamReader(wResp.GetResponseStream()))
{
result = sr.ReadToEnd();
sr.Close(); // BREAKPOINT 1
}
MessageBox.Show(cookieJar.Count + " cookies"); // 6 cookies.
wReq
= (HttpWebRequest)HttpWebRequest.Create("http://www.facebook.com/home.php");
wReq.Proxy = HttpWebRequest.GetSystemWebProxy();
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
wReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 "
+ "(KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24";
wReq.CookieContainer = cookieJar;
result = string.Empty;
wResp = (HttpWebResponse)wReq.GetResponse();
using (StreamReader sr = new StreamReader(wResp.GetResponseStream()))
{
result = sr.ReadToEnd();
sr.Close(); // BREAKPOINT 2
}
如果我在断点 1 处检查 result
,我可以看到结果包含 Facebook 登录页面的 html 代码。它告诉我,尽管我的 cookiaJar
已捕获 6 个 cookie,但我的浏览器中未启用 cookie。
同样,如果我在断点 2 处检查 result
,我可以看到 html 代码,它告诉我“我必须登录才能查看此页面”。
现在,我知道 Facebook 无需 JavaScript 即可运行,并且我正在 cookieJar
中捕获 6 个 cookie。我有点困惑为什么这不起作用?
(顺便问一下,我的密码有大写、小写、数字和符号。我需要用 URLEncode()
对其进行编码吗?我已经使用明文密码成功登录到另一个网站,而无需对密码进行编码。)
I'm writing a little test snippet of C# code which logs into Facebook and captures cookies. I'm doing it for educational purposes only. I have been given a task to automate form uploads, downloads, and form submissions between my work and a third party, and it's one of those things where I have to get it right, and I have to get it right the first time; hence why I thought I'd do a little practice with Facebook and some other web services.
I have:
string postMessage = "user=" + username + "&pass=" + password;
CookieContainer cookieJar = new CookieContainer();
MessageBox.Show(cookieJar.Count + " cookies"); // 0 cookies, as expected.
HttpWebRequest wReq
= (HttpWebRequest)HttpWebRequest.Create("https://www.facebook.com/login.php");
wReq.Proxy = HttpWebRequest.GetSystemWebProxy();
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
wReq.UserAgent
= "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 "
+ "(KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24";
wReq.CookieContainer = cookieJar;
wReq.Method = "POST";
wReq.ContentLength = postMessage.Length;
wReq.ContentType = "application/x-www-form-urlencoded";
StreamWriter sw = null;
try
{
sw = new StreamWriter(wReq.GetRequestStream());
sw.Write(postMessage);
}
catch
{
return false;
}
finally
{
sw.Close();
}
string result = string.Empty;
HttpWebResponse wResp = (HttpWebResponse)wReq.GetResponse();
using (StreamReader sr = new StreamReader(wResp.GetResponseStream()))
{
result = sr.ReadToEnd();
sr.Close(); // BREAKPOINT 1
}
MessageBox.Show(cookieJar.Count + " cookies"); // 6 cookies.
wReq
= (HttpWebRequest)HttpWebRequest.Create("http://www.facebook.com/home.php");
wReq.Proxy = HttpWebRequest.GetSystemWebProxy();
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
wReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 "
+ "(KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24";
wReq.CookieContainer = cookieJar;
result = string.Empty;
wResp = (HttpWebResponse)wReq.GetResponse();
using (StreamReader sr = new StreamReader(wResp.GetResponseStream()))
{
result = sr.ReadToEnd();
sr.Close(); // BREAKPOINT 2
}
If I check result
at breakpoint 1, I can see result contains the html code for Facebook's log in page. It is telling me that cookies are not enabled in my browser, even though my cookiaJar
has captured 6 cookies.
Similarly, if I check result
at breakpoint 2, I can see html code which is telling me "I have to log in to see this page".
Now, I know Facebook works without JavaScript, and I am capturing 6 cookies in my cookieJar
. I'm a little confused why this isn't working?
(By the way, my password has upper case, lower case, numbers and symbols. Do I need to encode it with URLEncode()
? I have successfully logged into a different website using a plain text password without encoding the password.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从用于登录的 https 切换为用于读取的 http。尝试将第二个读取更改为也使用 https,看看是否有帮助。
You switch from https for the login to http for the read. Try changing the second read to use https as well and see if that helps.