如何用cookie容器做多个帖子请求c# [英] how to do multiple post requests with cookie containers c#

查看:189
本文介绍了如何用cookie容器做多个帖子请求c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一个会话上发布2个帖子请求,但第二个请求总是给我主页的html源代码...

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个函数完全相同:执行post请求并将其添加到cookie容器中。
在第二个函数结束时,respontring字符串向我发送主页的html源页面,而不是之前的那个页面。
而在第一个帖子请求中的响应字符串(当我之前尝试过它)时,我发送了一个好的html源页面。

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.

这是我的代码:

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!

推荐答案

感谢@Hesam Fraridmehr这里的答案是:

Thanks to @Hesam Fraridmehr here is the answer:

添加:&到这样的一行:
-postData + =& style =+ data_style_id;

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

希望它有助于其他人

这篇关于如何用cookie容器做多个帖子请求c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆