简体   繁体   English

如何使用Cookie容器执行多个发布请求C#

[英]how to do multiple post requests with cookie containers c#

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... 我正在尝试在同一会话上执行2个发布请求,但是第二个请求总是给我主页的html源代码...

The 2 functions does exactly the same thing : do a post request and add it to the cookie container. 这两个函数的作用完全相同:执行发布请求并将其添加到Cookie容器中。 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. 在第二个函数的末尾,响应字符串向我发送了主页的html源页面,而不是以前的页面。 Whereas the responsetring (when I tried it before) in the 1st post request sends me the good html source page. 而第一个发布请求中的responsetring(在我之前尝试过的时候)向我发送了很好的html源页面。

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! 非常感谢你!

Thanks to @Hesam Fraridmehr here is the answer: 感谢@Hesam Fraridmehr,这是答案:

add: & to the line like this: -postData += "&style=" + data_style_id; 在这样的行中添加:&:-postData + =“&style =” + data_style_id;

hope it helps others 希望对别人有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM