C#HttpWebRequest访问RESTful Web服务

时间:2011-08-14 22:17:53

标签: c# ruby web-services httpwebrequest

我有一个用jRuby编写的REST Web服务,其入口点为http://localhost:4567/v4/start.htm

Web服务从SQL服务器下载数据并将其发送给客户端。

如何使用C#和httpWebrequest访问Web服务提供的功能。

谢谢

1 个答案:

答案 0 :(得分:1)

一般来说,你会做这样的事情:

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();
   }
}

我还没有混合使用RoR和C#(更不用说jRuby了),但它应该只是上面的一个基本修改。