I'm trying to do 2 post requests on the same session but the second one always gives me the html source code of the home page...
The 2 functions does exactly the same thing : do a post request and add it to the cookie container. At the end of the 2nd function, the responsestring sends me the html source page of the home page and not the one I was before. Whereas the responsetring (when I tried it before) in the 1st post request sends me the good html source page.
Here is my code:
private CookieContainer cookieContainer;
private void SendRequest_add_to_cart(string url, string data_style_id, string size)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
if (this.cookieContainer != null)
request.CookieContainer = this.cookieContainer;
else
request.CookieContainer = new CookieContainer();
var postData = "utf8=✓";
postData += "style=" + data_style_id;
postData += "size=" + size;
postData += "commit=add to basket";
var data = Encoding.ASCII.GetBytes(postData);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
this.cookieContainer = request.CookieContainer;
}
private void SendRequest_checkout(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
if (this.cookieContainer != null)
request.CookieContainer = this.cookieContainer;
else
request.CookieContainer = new CookieContainer();
var postData = "utf8=✓";
postData += "order[billing_name]=toto";
postData += "order[email]=toto@gmail.com";
var data = Encoding.ASCII.GetBytes(postData);
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
this.cookieContainer = request.CookieContainer;
Console.WriteLine(responseString);
}
here is my function who launches the one before:
var url_add_to_cart = link_general + doc.DocumentNode.SelectSingleNode("//form").Attributes["action"].Value;
var url_checkout = link_general + "/checkout.json";
SendRequest_add_to_cart(url_add_to_cart, data_style_id, size);
SendRequest_checkout(url_checkout);
If someone has an idea to help me it would be great! thank you very much!