c# HttpWebRequest访问RESTful web服务

本文关键字:web 服务 RESTful 访问 HttpWebRequest | 更新日期: 2023-09-27 18:05:27

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

web service从SQL server下载数据并发送给客户端。

如何使用c#和httpWebrequest访问web服务提供的函数

谢谢

c# HttpWebRequest访问RESTful web服务

一般来说,你会这样做:

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了),但它应该只是上面的一个基本修改。