C # simple example of Post data and receiving

Source: Internet
Author: User

C # simple example of Post data and receiving public partial class Post_Server: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {string type = ""; string Re = ""; Re + = "data transfer method:"; if (Request. requestType. toUpper () = "POST") {type = "POST"; Re + = type + "<br/> the parameters are: <br/> "; sortedList table = Param (); if (table! = Null) {foreach (DictionaryEntry De in table) {Re + = "parameter name:" + De. key + "value:" + De. value + "<br/>" ;}} else {Re = "you didn't pass any parameters! ";}} Else {type =" GET "; Re + = type +" <br/> the parameters are: <br/> "; NameValueCollection nvc = GETInput (); if (nvc. count! = 0) {for (int I = 0; I <nvc. count; I ++) {Re + = "parameter name:" + nvc. getKey (I) + "value:" + nvc. getValues (I) [0] + "<br/>" ;}} else {Re = "you didn't pass any parameters! ";}} Response. write (Re);} // GET the data returned by GET private NameValueCollection GETInput () {return Request. queryString;} // obtain the private string PostInput () {try {System. IO. stream s = Request. inputStream; int count = 0; byte [] buffer = new byte [1024]; StringBuilder builder = new StringBuilder (); while (count = s. read (buffer, 0, 1024)> 0) {builder. append (Encoding. UTF8.GetString (buffer, 0, coun T);} s. flush (); s. close (); s. dispose (); return builder. toString () ;}catch (Exception ex) {throw ex ;}} private SortedList Param () {string POSTStr = PostInput (); SortedList SortList = new SortedList (); int index = POSTStr. indexOf ("&"); string [] Arr ={}; if (index! =-1) // parameter passing is not only one {Arr = POSTStr. split ('&'); for (int I = 0; I <Arr. length; I ++) {int partition Index = Arr [I]. indexOf ('='); string paramN = Arr [I]. substring (0, reverse index); string paramV = Arr [I]. substring (distinct index + 1); if (! SortList. containsKey (paramN) // prevents users from passing the same parameter {SortList. add (paramN, paramV);} else // if there are the same, keep deleting and take the last value as the standard {SortList. remove (paramN); SortList. add (paramN, paramV) ;}} else // The value of the parameter is less than or equal to 1 {int reverse Index = POSTStr. indexOf ('='); if (partial index! =-1) {// The parameter is a string paramN = POSTStr. substring (0, reverse index); string paramV = POSTStr. substring (distinct index + 1); SortList. add (paramN, paramV);} else // No parameter passed over {SortList = null;} return SortList ;}} protected void button#click (object sender, EventArgs e) {Encoding encode = System. text. encoding. getEncoding ("UTF-8"); byte [] arrB = encode. getBytes ("aa = aa & bb = Hafei"); HttpWebRequest myReq = (HttpWebRequest) WebRequest. create ("http: // localhost: 11626/MyTest/Post_Server.aspx"); myReq. method = "POST"; myReq. contentType = "application/x-www-form-urlencoded"; myReq. contentLength = arrB. length; Stream outStream = myReq. getRequestStream (); outStream. write (arrB, 0, arrB. length); outStream. close (); // receives the HTTP Response WebResponse myResp = myReq. getResponse (); Stream ReceiveStream = myResp. getResponseStream (); StreamReader readStream = new StreamReader (ReceiveStream, encode); Char [] read = new Char [256]; int count = readStream. read (read, 0,256); string str = null; www.2cto.com while (count> 0) {str + = new String (read, 0, count); count = readStream. read (read, 0,256);} readStream. close (); myResp. close (); Response. write (str );}

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.