In the actual programming, may need to read the specific webpage information, but many websites need the user to log in, only then can obtain the related page content, this needs the programmer to temporarily store the current cookie,
In C #, you can use the Cookiecontainer object to hold the cookie information after logging in, so that when you send the data, the cookie information is appended to it.
#regionSynchronous sending of data via post/// <summary> ///send data via post/// </summary> /// <param name= "Url" >URL</param> /// <param name= "Postdatastr" >Post Data</param> /// <param name= "cookies" >Cookie Container</param> /// <returns></returns> Public stringSenddatabypost (stringUrlstringPOSTDATASTR,refCookiecontainer Cookies) {HttpWebRequest Request=(HttpWebRequest) webrequest.create (URL); if(cookies.) Count = =0) {request. Cookiecontainer=NewCookiecontainer (); Cookies=request. Cookiecontainer; } Else{request. Cookiecontainer=cookies; } request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; Request. ContentLength=postdatastr.length; Stream Myrequeststream=request. GetRequestStream (); StreamWriter Mystreamwriter=NewStreamWriter (Myrequeststream, Encoding.GetEncoding ("gb2312")); Mystreamwriter.write (POSTDATASTR); Mystreamwriter.close (); HttpWebResponse Response=(HttpWebResponse) request. GetResponse (); Stream Myresponsestream=Response. GetResponseStream (); StreamReader Mystreamreader=NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); stringretstring=Mystreamreader.readtoend (); Mystreamreader.close (); Myresponsestream.close (); returnretstring; } #endregion #regionSynchronous sending of data by Get mode/// <summary> ///send data by Get mode/// </summary> /// <param name= "Url" >URL</param> /// <param name= "Postdatastr" >Get Data</param> /// <param name= "cookies" >Get Container</param> /// <returns></returns> Public stringSenddatabyget (stringUrlstringPOSTDATASTR,refCookiecontainer Cookies) {HttpWebRequest Request= (HttpWebRequest) webrequest.create (Url + (Postdatastr = =""?"":"?") +postdatastr); if(cookies.) Count = =0) {request. Cookiecontainer=NewCookiecontainer (); Cookies=request. Cookiecontainer; } Else{request. Cookiecontainer=cookies; } request. Method="GET"; Request. ContentType="Text/html;charset=utf-8"; HttpWebResponse Response=(HttpWebResponse) request. GetResponse (); Stream Myresponsestream=Response. GetResponseStream (); StreamReader Mystreamreader=NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); stringRetString =Mystreamreader.readtoend (); Mystreamreader.close (); Myresponsestream.close (); returnretstring; } #endregionoperation code Cookiecontainer CC=NewCookiecontainer (); cc. ADD (NewSystem.Uri ("http://www.baidu.com"),NewCookies ("Phpsessid","xx"));stringContent = web. Senddatabyget ("http://www.baidu.com/xx/yy.php","","GBK",refcc); MessageBox.Show (content);
C # with a cookie post and get mode to send data, keep cookies