how to do multiple post requests with cookie containers c#

╄→尐↘猪︶ㄣ 提交于 2019-12-24 10:15:45

问题


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!


回答1:


Thanks to @Hesam Fraridmehr here is the answer:

add: & to the line like this: -postData += "&style=" + data_style_id;

hope it helps others



来源:https://stackoverflow.com/questions/50587841/how-to-do-multiple-post-requests-with-cookie-containers-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!