Several ways to invoke the service side in C # (Http+post) clients

Source: Internet
Author: User

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

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.