http - C# Logging in to a website with cookies -


i've been trying program log in website few days now, no avail. believe have bit of correct (particularly format of data needs posted).

however, i'm having hard time figuring out problem i'm having. website requires cookies authenticate login , i'm not sure how handle cookies. i've tried following many google results it's silly, , yet none of them great job of explaining need know.

to achieve log in, i'm using httpwebrequest , httpwebresponse. code i'm using put other stack overflow questions did not solve problem. means, essentially, not understand process required log website http. have fiddler installed, , have used chrome developers tools monitor network traffic , compare post of website.

here code:

using system; using system.net; using system.io; using system.text; using system.threading.tasks; using system.net.http;  namespace formposting {     class program     {         static void main(string[] args)         {             // url of login form , data needs posted             string formurl = "https://www.pogo.com/action/pogo/login.do";             string postdata = "screenname=" + "username" + "&password=" + "password" + "&remember_password_hidden=" + "true" + "&signin=" + "sign in";             byte[] databytes = utf8encoding.utf8.getbytes(postdata);              var request = (httpwebrequest)webrequest.create(formurl);             request.cookiecontainer = new cookiecontainer();             request.keepalive = true;             request.method = "post";             request.contenttype = "application/x-www-form-urlencoded";             request.contentlength = databytes.length;              using (stream poststream = request.getrequeststream())             {                 poststream.write(databytes, 0, databytes.length);             }              httpwebresponse response = (httpwebresponse)request.getresponse();         }     } 

now, what's happening uri page returning page: http://pogo.com/error/no-cookies.jsp

it doesn't matter username , password because returns page. never gets point can attempt log in.

could give me more insight on how works , how achieve successful login?

you have persist cookie jar cookiecontainer across requests. each time cookie jar destroyed cookies in destroyed too. right have tied lifetime of jar lifetime of single request.

correct so:

namespace formposting {     class program     {         static cookiecontainer cookiejar = new cookiecontainer();         static void main(string[] args)             request.cookiecontainer = cookiejar;         }     } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -