C # tips for simulating post and get Data Transmission
Source: Internet
Author: User
Through the application Program When reading webpage information, you usually need to capture webpage data, but there is a problem that many webpages need to log on to obtain page data, so you need to save the current cookie in. net, you can use the cookiecontainer object to save the cookie information after logon. This problem can be solved by adding cookie information each time data is sent. # Region synchronously sends data via post // <summary> // send data via post /// </Summary> /// <Param name = "url"> URL </param> /// <Param name = "postdatastr"> Post Data </param> /// <Param name = "cookie"> cookie container </param>/ // <returns> </returns> Public String senddatabypost (string URL, string postdatastr, ref cookiecontainer cookie) {httpwebrequest request = (httpwebrequest) webrequest. create (URL); If (cookie. count = 0) {request. Cookiecontainer = new cookiecontainer (); cookie = request. cookiecontainer;} else {request. cookiecontainer = cookie;} request. method = "Post"; request. contenttype = "application/X-WWW-form-urlencoded"; request. contentlength = postdatastr. length; stream myrequeststream = request. getrequeststream (); streamwriter mystreamwriter = new streamwriter (myrequeststream, encoding. getencoding ("gb2312"); m Ystreamwriter. write (postdatastr); mystreamwriter. close (); httpwebresponse response = (httpwebresponse) request. getresponse (); stream myresponsestream = response. getresponsestream (); streamreader mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("UTF-8"); string retstring = mystreamreader. readtoend (); mystreamreader. close (); myresponsestream. close (); Return retstring ;}# endh Gion # region synchronously sends data in get mode /// <summary> /// send data in get mode /// </Summary> /// <Param name = "url"> URL </param> /// <Param name = "postdatastr"> get data </param> /// <Param name = "cookie"> Get container </param> /// <returns> </returns> Public String senddatabyget (string URL, string postdatastr, ref cookiecontainer cookie) {httpwebrequest request = (httpwebrequest) webrequest. create (URL + (postdatastr = ""? "":"? ") + Postdatastr); If (cookie. count = 0) {request. cookiecontainer = new cookiecontainer (); cookie = request. cookiecontainer;} else {request. cookiecontainer = cookie;} request. method = "get"; request. contenttype = "text/html; charset = UTF-8"; httpwebresponse response = (httpwebresponse) request. getresponse (); stream myresponsestream = response. getresponsestream (); streamreader mystreamreader = new streamreader (myresponsestream, encoding. getencoding ("UTF-8"); string retstring = mystreamreader. readtoend (); mystreamreader. close (); myresponsestream. close (); Return retstring ;}# endregion
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