C# web server doesn't see stored cookies
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 = (HttpW开发者_开发技巧ebResponse)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.)
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.
精彩评论