Restclient client sends HTTP request code

Source: Internet
Author: User
Tags baseuri

  Public classrestclient {Private stringBaseUri;  PublicRestclient (stringBaseUri) {             This. BaseUri =BaseUri; }        #regionDelete mode Public stringDelete (stringDatastringURI) {            returnCommonhttprequest (data, URI,"DELETE"); }         Public stringDelete (stringURI) {            //Web Access Objects            stringserviceurl =string. Format ("{0}/{1}", This.            BaseUri, URI); HttpWebRequest Myrequest=(HttpWebRequest) webrequest.create (serviceurl); Myrequest.method="DELETE"; //Get interface return valueHttpWebResponse Myresponse =(HttpWebResponse) myrequest.getresponse (); StreamReader Reader=NewStreamReader (Myresponse.getresponsestream (), Encoding.UTF8); //string returnxml = Httputility.urldecode (reader. ReadToEnd ());            stringReturnxml =Reader.            ReadToEnd (); Reader.            Close ();            Myresponse.close (); returnReturnxml; }        #endregion        #regionPut mode Public stringPut (stringDatastringURI) {            returnCommonhttprequest (data, URI,"PUT"); }        #endregion        #regionPost Mode implementation Public stringPost (stringDatastringURI) {            returnCommonhttprequest (Data,uri,"POST"); }         Public stringCommonhttprequest (stringDatastringUristringtype) {            //Web Access object, constructing the requested URL address            stringserviceurl =string. Format ("{0}/{1}", This.            BaseUri, URI); //object that constructs an HTTP requestHttpWebRequest Myrequest =(HttpWebRequest) webrequest.create (serviceurl); //turn into a network stream            byte[] buf = System.Text.Encoding.GetEncoding ("UTF-8").            GetBytes (data); //SetMyrequest.method =type; Myrequest.contentlength=buf.            Length; Myrequest.contenttype="Application/json"; Myrequest.maximumautomaticredirections=1; Myrequest.allowautoredirect=true; //Send RequestStream newstream =Myrequest.getrequeststream (); Newstream.write (BUF,0, buf.            Length);            Newstream.close (); //Get interface return valueHttpWebResponse Myresponse =(HttpWebResponse) myrequest.getresponse (); StreamReader Reader=NewStreamReader (Myresponse.getresponsestream (), Encoding.UTF8); stringReturnxml =Reader.            ReadToEnd (); Reader.            Close ();            Myresponse.close (); returnReturnxml; }        #endregion        #regionGet mode implementation Public stringGet (stringURI) {            //Web Access Objects            stringserviceurl =string. Format ("{0}/{1}", This.                       BaseUri, URI); //constructs an object for a Web requestHttpWebRequest Myrequest =(HttpWebRequest) webrequest.create (serviceurl); //Get interface return value//gets the content of the response for the Web requestHttpWebResponse Myresponse =(HttpWebResponse) myrequest.getresponse (); //construct a StreamReader with a response streamStreamReader reader =NewStreamReader (Myresponse.getresponsestream (), Encoding.UTF8); //string returnxml = Httputility.urldecode (reader. ReadToEnd ());            stringReturnxml =Reader.            ReadToEnd (); Reader.            Close ();            Myresponse.close (); returnReturnxml; }        #endregion    }

Restclient client sends HTTP request code

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.