c# - Request and Response Cookies doesn't work in a Console app -
i have windows console works fine when make httpwebrequest user, if try other user, it's not works. think problem in cookies, because obtain less values when use user not works when use user works.
here's code:
mainconsole:
namespace consoleapplication1 { class program { static void main(string[] args) { var mymanager = new requestmanager(); mymanager.sendpostrequest("http://www.example.com","", true); var myresponse = mymanager.sendpostrequest("http://www.example.com/login.phtml", "login=username&pass=password&action=login&%3e%3e+login_x=33", true); var content = mymanager.getresponsecontent(myresponse); console.write(content); console.readline(); } } } requestmanager class:
namespace consoleapplication1 { public class requestmanager { public string lastresponse { protected set; get; } cookiecontainer cookies = new cookiecontainer(); internal string getcookievalue(uri siteuri, string name) { cookie cookie = cookies.getcookies(siteuri)[name]; return (cookie == null) ? null : cookie.value; } public string getresponsecontent(httpwebresponse response) { if (response == null) { throw new argumentnullexception("response"); } stream datastream = null; streamreader reader = null; string responsefromserver = null; try { // stream containing content returned server. datastream = response.getresponsestream(); // open stream using streamreader easy access. reader = new streamreader(datastream); // read content. responsefromserver = reader.readtoend(); // cleanup streams , response. } catch (exception ex) { console.writeline(ex.message); } { if (reader != null) { reader.close(); } if (datastream != null) { datastream.close(); } response.close(); } lastresponse = responsefromserver; return responsefromserver; } public httpwebresponse sendpostrequest(string uri, string content, bool allowautoredirect) { httpwebrequest request = generatepostrequest(uri, content, allowautoredirect); return getresponse(request); } public httpwebresponse sendrequest(string uri, string content, string method, bool allowautoredirect) { httpwebrequest request = generaterequest(uri, content, method, allowautoredirect); return getresponse(request); } public httpwebrequest generatepostrequest(string uri, string content, bool allowautoredirect) { return generaterequest(uri, content, "post", allowautoredirect); } internal httpwebrequest generaterequest(string uri, string content, string method, bool allowautoredirect) { if (uri == null) { throw new argumentnullexception("uri"); } // create request using url can receive post. httpwebrequest request = (httpwebrequest)httpwebrequest.create(uri); // set method property of request post. request.method = method; // set cookie container maintain cookies request.cookiecontainer = cookies; request.allowautoredirect = allowautoredirect; if (method == "post") { // convert post data byte array. byte[] bytearray = encoding.utf8.getbytes(content); // set contenttype property of webrequest. request.contenttype = "application/x-www-form-urlencoded"; // set contentlength property of webrequest. request.contentlength = bytearray.length; // request stream. stream datastream = request.getrequeststream(); // write data request stream. datastream.write(bytearray, 0, bytearray.length); // close stream object. datastream.close(); } return request; } internal httpwebresponse getresponse(httpwebrequest request) { if (request == null) { throw new argumentnullexception("request"); } httpwebresponse response = null; try { response = (httpwebresponse)request.getresponse(); cookies.add(response.cookies); // print properties of each cookie. console.writeline("\ncookies: "); foreach (cookie cook in cookies.getcookies(request.requesturi)) { console.writeline("domain: {0}, string: {1}", cook.domain, cook.tostring()); } } catch (webexception ex) { console.writeline("web exception occurred. status code: {0}", ex.status); } catch (exception ex) { console.writeline(ex.message); } return response; } } } edit: paste headers can see in fiddler :)
headers of not working user:
response sent 81 bytes of cookie data: set-cookie: language=es_es; expires=tue, 05-jun-2012 16:24:58 gmt; path=/; domain=.comunio.es response sent 30 bytes of cookie data: set-cookie: session_language=es_es; path=/ headers of valid user:
response sent 81 bytes of cookie data: set-cookie: language=es_es; expires=tue, 05-jun-2012 16:27:12 gmt; path=/; domain=.comunio.es response sent 30 bytes of cookie data: set-cookie: session_language=es_es; path=/ response sent 190 bytes of cookie data: set-cookie: phpbb2mysql_data=a%3a2%3a%7bs%3a11%3a%22autologinid%22%3bs%3a0%3a%22%22%3bs%3a6%3a%22userid%22%3bs%3a7%3a%225638420%22%3b%7d; expires=tue, 04-jun-2013 16:27:12 gmt; path=/; domain=comunio.es response sent 75 bytes of cookie data: set-cookie: phpbb2mysql_sid=e0e1f8e93e57afe267d240da4b596130; path=/; domain=comunio.es response sent 94 bytes of cookie data: set-cookie: c=e2d6059d1f3a7871d99cc6a17883b198bf0f223d672e21b2fa9a16e8f9e74a63; path=/; domain=.comunio.es
Comments
Post a Comment