C # Analog Post request method
Method 1:
<summary>///analog POST request///</summary>//<param name= "url" > Address </param> <param name= "Postdatastr" > Parameters:a=123&b=www</param>//<returns></returns> public string httppost (string url, string postdatastr) {Encoding encode = System.Text.Encoding.GetEnc Oding ("Utf-8"); byte[] Arrb = encode. GetBytes (POSTDATASTR); HttpWebRequest Myreq = (HttpWebRequest) webrequest.create (URL); 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 (); Response received by HTTP 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; while (Count > 0) {str + = new String (read, 0, Count); Count = Readstream.read (Read, 0, 256); } readstream.close (); Myresp.close (); return str; }
Method 2:
<summary>///Send a POST request///</summary>//<param name= "url" ></param>//<param Name= "POSTDATASTR" ></param>///<returns></returns> public static string HttpPost (string URL, String postdatastr) {HttpWebRequest request = (HttpWebRequest) webrequest.create (URL); Request. Method = "POST"; Request. ContentType = "application/x-www-form-urlencoded"; Request. ContentLength = Encoding.UTF8.GetByteCount (POSTDATASTR); Stream Myrequeststream = Request. GetRequestStream (); StreamWriter mystreamwriter = new StreamWriter (Myrequeststream, encoding.getencoding ("gb2312")); Mystreamwriter.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; }
C # Back-end Post request method