. NET calling Java Rest ful API instances

Source: Internet
Author: User
Tags http authentication urlencode

Note the combination of the parameters of the post

HttpWebRequest request = webrequest.create (URL) asHttpwebrequest;request. Method="POST"; request. KeepAlive=true; request. AllowAutoRedirect=false; request. ContentType="application/x-www-form-urlencoded";byte[] Postdatabtyes = Encoding.UTF8.GetBytes ("param={"Imsi":"460023887603775","Session":"Hwjpcfmgnrtyoed91itwptecy"}"); request. ContentLength=Postdatabtyes. Length; Stream Requeststream=request. GetRequestStream (); Requeststream. Write (Postdatabtyes,0, Postdatabtyes. Length); Requeststream. Close ();stringresp;Try{   using(HttpWebResponse response = Request.) GetResponse () asHttpWebResponse) {StreamReader SR=NewStreamReader (response.      GetResponseStream (), Encoding.UTF8); Resp=Sr.    ReadToEnd (); }   }

Another type of:

2, post to getusingsystem.web; Uri Address=NewUri ("http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction"); //Create the Web requestHttpWebRequest request = WebRequest.Create (address) asHttpWebRequest; //Set type to POSTRequest. Method ="POST"; Request. ContentType="application/x-www-form-urlencoded"; //Create The data we want to send    stringAppId ="Yahoodemo"; stringContext ="Italian sculptors and painters of the Renaissance"+"favored the Virgin Mary for inspiration"; stringquery ="Madonna"; StringBuilder Data=NewStringBuilder (); Data. Append ("appid="+Httputility.urlencode (appId)); Data. Append ("&context="+Httputility.urlencode (context)); Data. Append ("&query="+httputility.urlencode (query)); //Create A byte array of the data we want to send    byte[] Bytedata =UTF8Encoding.UTF8.GetBytes (data.  ToString ()); //Set The content length in the request headersRequest. ContentLength =bytedata.length; //Write Data    using(Stream Poststream =request. GetRequestStream ()) {Poststream.write (bytedata,0, bytedata.length); }//Get Response    using(HttpWebResponse response = Request.) GetResponse () asHttpWebResponse) {//Get the response streamStreamReader reader =NewStreamReader (response.  GetResponseStream ()); //Console Application OutputConsole.WriteLine (reader.  ReadToEnd ());  HTTP authentication request Some friends may want to provide the user name and password for HTTP authentication when calling the RESTful API, where we can do this by adding a networkcredentials instance to the request. //Create the Web requestHttpWebRequest Request= WebRequest.Create ("https://api.del.icio.us/v1/posts/recent") asHttpWebRequest; //ADD authentication to requestRequest. Credentials =NewNetworkCredential ("username","Password"); //Get Response    using(HttpWebResponse response = Request.) GetResponse () asHttpWebResponse) {//Get the response streamStreamReader reader =NewStreamReader (response.  GetResponseStream ()); //Console Application OutputConsole.WriteLine (reader.  ReadToEnd ()); }

. NET calling Java Rest ful API instances

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.