c# - 如何使用cookie容器执行多个发布请求

我试图在同一个会话上做 2 个 post 请求,但第二个总是给我主页的 html 源代码......


这两个函数做的完全一样:做一个 post 请求并将它添加到 cookie 容器中。在第二个函数结束时,响应字符串向我发送主页的 html 源页面,而不是我之前的那个。而第一个帖子请求中的 responsetring(当我之前尝试过时)向我发送了好的 html 源页面。


这是我的代码:


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);

    }


如果有人有想法来帮助我,那就太好了!非常感谢!


偶然的你
浏览 156回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP