如何使用 cookie 容器 c# 执行多个 post 请求
how to do multiple post requests with cookie containers c#
我试图在同一个会话中执行 2 个 post 请求,但第二个请求总是给我主页的 html 源代码...
这 2 个函数做完全相同的事情:做一个 post 请求并将其添加到 cookie 容器中。
在第二个函数的末尾,responsestring 向我发送了主页的 html 源页面,而不是我之前的那个。
而第一个 post 请求中的响应字符串(当我之前尝试过时)向我发送了良好的 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);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
this.cookieContainer = request.CookieContainer;
Console.WriteLine(responseString);
}
这是我启动之前的函数:
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);
如果有人有想法帮助我那就太好了!非常感谢!
感谢@Hesam Fraridmehr,这里是答案:
像这样在行中添加:&:
-postData += "&style=" + data_style_id;
希望对别人有帮助
我试图在同一个会话中执行 2 个 post 请求,但第二个请求总是给我主页的 html 源代码...
这 2 个函数做完全相同的事情:做一个 post 请求并将其添加到 cookie 容器中。 在第二个函数的末尾,responsestring 向我发送了主页的 html 源页面,而不是我之前的那个。 而第一个 post 请求中的响应字符串(当我之前尝试过时)向我发送了良好的 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);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
this.cookieContainer = request.CookieContainer;
Console.WriteLine(responseString);
}
这是我启动之前的函数:
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);
如果有人有想法帮助我那就太好了!非常感谢!
感谢@Hesam Fraridmehr,这里是答案:
像这样在行中添加:&: -postData += "&style=" + data_style_id;
希望对别人有帮助