I know there are many similar question here but I tried almost everything with no luck.
Basically, I've never used the POST method before, and I'm struggling to login to any website and maintain the cookies in order to use in next requests.
Here's my code (I'm using Facebook for testing only):
Login method:
protected static CookieContainer Login()
{
string userName = "username";
string password = "password";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "email=" + userName + "&pass=" + password;
byte[] postDataBytes = encoding.GetBytes(postData);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.facebook.com/login.php");
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = postDataBytes.Length;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36";
httpWebRequest.Proxy = null;
httpWebRequest.AllowAutoRedirect = false;
using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(postDataBytes, 0, postDataBytes.Length);
stream.Close();
}
var cookieContainer = new CookieContainer();
using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
foreach (Cookie cookie in httpWebResponse.Cookies)
{
cookieContainer.Add(cookie);
}
}
}
return cookieContainer;
}
Retrieve the homepage after logging in:
void Retrieve()
{
var req =(HttpWebRequest)WebRequest.Create("https://www.facebook.com");
req.Proxy = null;
req.KeepAlive = true;
req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36";
req.CookieContainer = Login();
string src = "";
using (var res = (HttpWebResponse)req.GetResponse())
{
src = new StreamReader(res.GetResponseStream()).ReadToEnd();
}
txt.Text = src;
wb.DocumentText = src;
}
I also tried to create the CookieContainer first and then assign it to both requests, but still no luck.
I would appreciate if someone can tell me what I'm missing.
Note: As mentioned, I'm using Facebook here just for testing. If required I can post the original website I'm trying to login to.
Update:
The actual website I'm trying to automate the login process for is: carrierpoint.com.
I have to say I don't really have much experience with web requests/responses, authentication, etc. I never worked with ASP.NET or any kind of web application, so please excuse my limited knowledge in this area.
So for more info, this is an example of request & response headers for a successful login to this website:
If this isn't just about cookies, I would appreciate if someone can help me figure out what other elements would be needed to submit the correct request and maintain the logged in session.
Also would be great if anyone can suggest some resources as a good start for me to generally understand the correct way(s) to use the POST method and website login & authentication (I mean the general concept to be able to use on other websites).
