C # with a cookie post and get mode to send data, keep cookies

Source: Internet
Author: User

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

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.