. NET program for post data to remain session after login

Source: Internet
Author: User

Recently found some interesting things on the internet, want to catch down, was a small problem to get stuck, the program how to send post requests are unable to obtain the desired results, using Firefox to carefully study the discovery need to open a page, and then submit parameters OK, this is good

Learning the programming of the web, we know that the session to maintain a conversational state, so that similar landing function can be maintained continuously.

However, the session is lost when the POST request is sent by the program.

The reason is still to see the principle of the session.

In fact, the session usually has a sessionid stored in a cookie.

Each request for data sends the last cookie to the server.

PHP is generally a phpsessionid

Asp. NET seems to be Aspnetsessionid

.....

In fact, as long as the request to return the Sesionid to save. The next assignment to the requested request object is OK, as long as the session is not expired on the server.

The following code is implemented:

Private StaticCookiecontainer M_cookie =NewCookiecontainer ();Private Static stringPoststringPostURL,stringpostdata, Encoding pageencoding)            {HttpWebRequest HttpWebRequest;            HttpWebResponse HttpWebResponse; byte[] Bytestopost =pageencoding.getbytes (postdata); Try{HttpWebRequest= WebRequest.Create (PostURL) asHttpWebRequest; Httpwebrequest.method="POST"; Httpwebrequest.keepalive=true; Httpwebrequest.contenttype="application/x-www-form-urlencoded"; Httpwebrequest.cookiecontainer= M_cookie;//set a cookie on the previous access page to keep sessionHttpwebrequest.contentlength =bytestopost.length; Stream Requeststream=Httpwebrequest.getrequeststream (); requestStream.Write (Bytestopost,0, bytestopost.length);//Write post informationRequeststream.close (); HttpWebResponse= HttpWebRequest.GetResponse () asHttpWebResponse; M_cookie= Httpwebrequest.cookiecontainer;//Update cookies after accessStream Responsestream =Httpwebresponse.getresponsestream (); stringResdata; using(StreamReader RESSR =NewStreamReader (Responsestream, pageencoding)) {Resdata=Ressr.readtoend ();                    Ressr.close ();                Responsestream.close (); }                returnResdata; }            Catch(Exception err) {Throwerr; }

. NET program for post data to remain session after login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.