简体   繁体   中英

C# HttpWebRequest accessing RESTful web service

I have a REST web service written in jRuby with entry point http://localhost:4567/v4/start.htm

The web service downloads data from SQL server and sends it to a client.

How do I use C# and httpWebrequest to access the functions provided by the web service.

Thank you

Generally speaking you're going to do something like this:

HttpWebRequest Request = WebRequest.Create(Url) as HttpWebRequest;
Request.Method = "GET"; //Or PUT, DELETE, POST
Request.ContentType = "application/x-www-form-urlencoded";
using (HttpWebResponse Response = Request.GetResponse() as HttpWebResponse)
{
   if (Response.StatusCode != HttpStatusCode.OK)
      throw new Exception("The request did not complete successfully and returned status code " + Response.StatusCode);
   using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
   {
      string ReturnedData=Reader.ReadToEnd();
   }
}

I haven't mixed RoR and C# yet (let alone jRuby), but it should be just a basic modification of the above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM