我正在处理另一个使用http网络请求的项目。 目前我在某些请求中使用cookie容器,但我发现它并不适用于此特定请求。 我让网站通过http查询找到它所找到的每个页面并从中下载我想要的信息,但是因为页面网址被隐藏了,如果你按回去浏览器它在进入主页之前没有进入该页面。
我发现请求的主体可以更改页面,当我使用我使用fiddler的cookie时它可以正常工作。但是,由于我尝试了cookiecontainer,所有页面上的信息都与第一页相同。
for (int i = 2; i <= _TotalPag; i++)
{
try
{
int _pagina = i;
HttpWebResponse response3;
HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(lista_links_categorias[cbo_categorias.SelectedIndex]);
request3.KeepAlive = true;
request3.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
request3.Accept = "*/*";
request3.Referer = "xxxxxxxxxxx";
request3.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.8,pt;q=0.6");
CookieContainer cookieContainer3 = new CookieContainer();
request3.CookieContainer = cookieContainer3;
request3.Method = "POST";
request3.ServicePoint.Expect100Continue = false;
string _body;
if (i == 2)
{
_body = @"M%24M%24sM1=M%24M%24sM1%7CM%24M%24lPH%24lCPH%24cCatBrw%24cPL%24btPagCommd&__EVENTTARGET=M%24M%24lPH%24lCPH%24cCatBrw%24cPL%24btPagCommd&__EVENTARGUMENT=&__VIEWSTATE_KEY=cafb3561-af6f-4e78-bc31-b71166377d8e&__VIEWSTATE=&txtQ=&orderby=DisplayName&orderby=ASC&produtos=21&orderby=DisplayName&orderby=ASC&produtos=21&hdnPrdListData=1%24%242&hdnCatalogBrowser=&txtEmail=&txtPass=&__ASYNCPOST=true&";
}
else
{
_body = @"M%24M%24sM1=M%24M%24sM1%7CM%24M%24lPH%24lCPH%24cCatBrw%24cPL%24btPagCommd&__VIEWSTATE=&txtQ=&orderby=DisplayName&orderby=ASC&produtos=21&orderby=DisplayName&orderby=ASC&produtos=21&hdnPrdListData=1%24%24" + _pagina + "&hdnCatalogBrowser=&txtEmail=&txtPass=&__EVENTTARGET=M%24M%24lPH%24lCPH%24cCatBrw%24cPL%24btPagCommd&__EVENTARGUMENT=&__VIEWSTATE_KEY=cafb3561-af6f-4e78-bc31-b71166377d8e&__ASYNCPOST=true&";
}
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(_body);
request3.ContentLength = postBytes.Length;
Stream stream = request3.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
using (response3 = (HttpWebResponse)request3.GetResponse())
{
var responseValue = string.Empty;
// grab the response
using (var responseStream = response3.GetResponseStream())
{
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
if (responseValue != "")
{
string _txtFileNew = @"xxxxxxxxxx\Pagina" + _pagina + ".txt";
StreamWriter _srEannew = new StreamWriter(_txtFileNew, true, Encoding.UTF8);
_srEannew.WriteLine(responseValue);
_srEannew.Close();
}
}
HtmlAgilityPack.HtmlDocument doc3 = new HtmlAgilityPack.HtmlDocument();
doc3.Load(@"xxxxxxxx\Pagina" + _pagina + ".txt");
HtmlNodeCollection collection2 = doc3.DocumentNode.SelectNodes(".//div[@class='produtoGrelha']");
foreach (HtmlNode node2 in collection2)
{
string _ID = node2.Attributes["p"].Value.Replace("/n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(" ", string.Empty).Trim();
lista_ids_prods.Add(_ID);
}
}
是否有任何解决方案无法让我每天都使用需要更新的特定Cookie?
非常感谢
答案 0 :(得分:0)
尝试在for循环之前初始化CookieContainer。
CookieContainer cookieContainer3 = new CookieContainer();
for (int i = 2; i <= _TotalPag; i++)
{
...
request3.CookieContainer = cookieContainer3;
...
}