String URL ="Http://XXXXXXXX"; WebClient WebClient=NewWebClient (); WEBCLIENT.HEADERS.ADD ("Content-type","application/x-www-form-urlencoded"); //Method One byte[] PostData = encoding.getencoding ("GBK"). GetBytes (str); byte[] responsedata = Webclient.uploaddata (URL,"POST", PostData); RET= Encoding.UTF8.GetString (ResponseData);//decoding
WebClient Data Stream
String URL ="http://xxxxx"; WebClient WebClient=NewWebClient (); WEBCLIENT.HEADERS.ADD ("Content-type","application/x-www-form-urlencoded"); //Method Two byte[] buffer =Encoding.UTF8.GetBytes (str); stringSend = encoding.getencoding ("GBK"). GetString (buffer); RET= webclient.uploadstring (URL,"POST", Send);
WebClient Pass String
-----------------------Method Three----------------------------//convert to byte array byte[] buffer =Encoding.Default.GetBytes (str); stringSend =Encoding.UTF8.GetString (buffer); byte[] PostData = encoding.getencoding ("GBK"). GetBytes (Send); //Create requestHttpWebRequest request =(HttpWebRequest) httpwebrequest.create (URL); Request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; Request. ContentLength=postdata.length; //Create an input streamStream Requeststream =request. GetRequestStream (); //send a request to the serverrequestStream.Write (PostData,0, postdata.length); Requeststream.close (); //Get server return informationHttpWebResponse response =(HttpWebResponse) request. GetResponse (); StreamReader Reader=NewStreamReader (response. GetResponseStream (), Encoding.UTF8); stringContent =Reader. ReadToEnd (); stringres =Reader. ReadToEnd (); Reader. Close (); Requeststream.close ();
HttpWebRequest Data Stream
Several ways to invoke the service side in C # (Http+post) clients