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