c# - Can't Use CookieContainer on my request, only hand made cookie works :\ -
i'm working on project use http web requests. i'm using cookie containers on requests, found doesn't work on specific request. i'm making site change it's page http request each page finds , download info want it, since page url hidden, , if press go in browser doesn't go page before goes homepage.
i found body of request can change page , worked when used cookie got using fiddler. since tried cookiecontainer info on pages same 1st page.
(int = 2; <= _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, 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 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); } }
is there solution doesn't make me use specific cookie needs updates everyday?
thank lot
try initializing cookiecontainer before for-loop.
cookiecontainer cookiecontainer3 = new cookiecontainer(); (int = 2; <= _totalpag; i++) { ... request3.cookiecontainer = cookiecontainer3; ... }
Comments
Post a Comment