c# - Get cookies from httpwebrequest -
i'm trying cookies website using code
cookiecontainer cookiejar = new cookiecontainer(); var request = (httpwebrequest)httpwebrequest.create("http://www.foetex.dk/ugenstilbud/pages/zmags.aspx"); request.cookiecontainer = cookiejar; var response = request.getresponse(); foreach (cookie c in cookiejar.getcookies(request.requesturi)) { console.writeline("cookie['" + c.name + "']: " + c.value); } console.readline();
the thing want display console.writeline, im not getting single of them.
the following example uses httpcookie class , properties read cookie specific name.
httpcookie mycookie = new httpcookie("mytestcookie"); mycookie = request.cookies["mytestcookie"]; // read cookie information , display it. if (mycookie != null) response.write("<p>"+ mycookie.name + "<p>"+ mycookie.value); else response.write("not found");
retrieve values in httpwebresponse.cookies property of httpwebresponse. in example, cookies retrieved , saved isolated storage:
private void readcallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; httpwebresponse response = (httpwebresponse) request.endgetresponse(asynchronousresult); using (isolatedstoragefile isf = isolatedstoragefile.getuserstoreforsite()) { using (isolatedstoragefilestream isfs = isf.openfile("cookieexcookies", filemode.openorcreate, fileaccess.write)) { using (streamwriter sw = new streamwriter(isfs)) { foreach (cookie cookievalue in response.cookies) { sw.writeline("cookie: " + cookievalue.tostring()); } sw.close(); } } } }
Comments
Post a Comment