C # tips for simulating Post and Get Data Transmission

Source: Internet
Author: User

When reading webpage information through an application, you usually need to capture webpage data. However, there is a problem that many webpages need to log on before obtaining page data. Therefore, 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

Related Article

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.