这是我第一个学习C#和Web服务的工作。我开始学习SOAP的学期,现在我正在学习REST。我听说REST是比较流行的一种。到目前为止,用户对我来说似乎很难,但我对它仍然很陌生。C#Restable Web服务返回int值
所以我想创造宁静的服务。实际上创建服务正在工作。我想从服务和我的客户端/控制台端存储该int值到一个int arrayList返回int值。
问题是剩下的服务正在返回整个xml字符串。像这样
<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">0</int>
我只想要0,所以我可以将该值存储在我的arrayList中。我觉得C#应该有一些简单的库或者一些让我能够在xml标签之间获取值的东西。
我写了测试代码,让我更好地了解如何从我的REST服务中获取数据。
这里是WCF服务合同中的代码(我们应该在这个任务中使用WCF,所以我在测试代码中使用它)。
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare)]
int getTrafficAccidentsByZip(int zCode);
[OperationContract]
[WebGet(UriTemplate = "latlonValues?lonVal={lonVal}&latVal={latVal}",
BodyStyle =WebMessageBodyStyle.Bare)]
int getTrafficAccidentByLonLat(double lonVal, double latVal);
// TODO: Add your service operations here
}
这里是合同的执行情况。就像我说的,这是测试代码。所以我只是回来0
public class Service1 : IService1
{
public int getTrafficAccidentsByZip(int zCode)
{
return 0;
}
public int getTrafficAccidentByLonLat(double lonVal, double latValue)
{
return 0;
}
}
这是我主要。你可以在下面看到我试图将字符串转换为int。这导致了一个错误。
string url = @"http://localhost:14722/Service1.svc/getTrafficAccidentsByZip";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
// int test = Convert.ToInt32(reader);
String json = reader.ReadToEnd();
// int test = Convert.ToInt32(json);
Console.WriteLine(json);
我觉得这应该很容易。但我一直很难找到如何在网上做这个简单的例子。
任何好的在线C#REST示例或youtube视频将不胜感激。以示例。我从例子中学习。