HttpPost Send request without parameters
1 /// <summary>2 ///POST request with no parameters3 /// </summary>4 Public voidHttppostnoparam ()5 {6 stringURL ="Request Address";7HttpWebRequest request =webrequest.createhttp (URL);8Request. Method ="POST";9Request. ContentType ="application/x-www-form-urlencoded";TenStream Myrequeststream =request. GetRequestStream (); OneStreamWriter Mystreamwriter =NewStreamWriter (Myrequeststream, Encoding.GetEncoding ("gb2312")); A mystreamwriter.close (); - myrequeststream.close (); - theHttpWebResponse response =(HttpWebResponse) request. GetResponse (); -Stream Myresponsestream =Response. GetResponseStream (); -StreamReader Mystreamreader =NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); - stringRetString =mystreamreader.readtoend (); + mystreamreader.close (); - myresponsestream.close (); +}
HttpPost way to send request with parameters
1 /// <summary>2 ///Post request with parameters
UserID, transflow parameter name3 /// </summary>4 Public voidHttppostparam ()5{stringURL ="Request Address";6HttpWebRequest request =webrequest.createhttp (URL);7Request. Method ="POST";8Request. ContentType ="application/x-www-form-urlencoded";9 stringparam ="userid=2098&transflow=2";Ten byte[] by; Oneby =System.Text.Encoding.UTF8.GetBytes (param); ARequest. ContentLength =by . Length; -Stream Myrequeststream =request. GetRequestStream (); -StreamWriter Mystreamwriter =NewStreamWriter (Myrequeststream, Encoding.GetEncoding ("gb2312")); theMyrequeststream.write (By,0, by. Length); - mystreamwriter.close (); - myrequeststream.close (); - + -HttpWebResponse response =(HttpWebResponse) request. GetResponse (); +Stream Myresponsestream =Response. GetResponseStream (); AStreamReader Mystreamreader =NewStreamReader (Myresponsestream, Encoding.GetEncoding ("Utf-8")); at stringRetString =mystreamreader.readtoend (); - mystreamreader.close (); - myresponsestream.close (); -}
HttpGet way to send a request
1 /// <summary>2 ///Get mode3 /// </summary>4 Public voidHttpGet ()5 {6 stringURL =the request address? id=2";7HttpWebRequest request =webrequest.createhttp (URL);8Request. Method ="Get";9Request. ContentType ="application/x-www-form-urlencoded";Ten OneHttpWebResponse response =(HttpWebResponse) request. GetResponse (); AStream Read =Response. GetResponseStream (); -StreamReader SM =NewStreamReader (read, System.Text.Encoding.UTF8); - stringReadString =SM. ReadToEnd (); the -}
C # Post Get method Send Request