我在 .NET4 和 Visual Studio 2010 中创建了一个简单的 WebAPI 服务。
我需要在 Windows CE / CF 3.5 应用程序中使用该服务
我确实可以使用 HttpWebRequest,但不确定这是否可行,或者我应该使用 RestSharp。有没有人有洞察力/经验可以帮助我做出决定?我宁愿尽可能不使用 3rd 方代码来避免它,但如果与“原始”产品相比有明显优势,我愿意这样做。
是否有人拥有或知道使用 CF 3.5 中的 HttpWebRequest 或 RestSharp 访问 WebApi 服务的示例?
我有此代码(改编自http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api)用于示例 WebAPI 方法:
public class VendorItemsController : ApiController
{
VendorItem[] vendorItems = new VendorItem[]
{
new VendorItem { VendorId = "1", VendorItemId = "Tomato Soup", ItemId = "Groceries", PackSize = 1 },
new VendorItem { VendorId = "2", VendorItemId = "V8", ItemId = "Groceries", PackSize = 6 },
new VendorItem { VendorId = "3", VendorItemId = "Garlic", ItemId = "Groceries", PackSize = 1 },
};
public IEnumerable<VendorItem> GetAllProducts()
{
return vendorItems;
}
public VendorItem GetProductById(string id)
{
var vendorItem = vendorItems.FirstOrDefault((p) => p.VendorId == id);
if (vendorItem == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return vendorItem;
}
}
...但如果可能的话,不知道如何使用 HttpWebRequest。
注意: HttpClient 对我不可用(但 HttpWebRequest 可用)。
更新
我启动了具有 WebAPI 方法的 VS2010 应用程序;但是当我使用以下代码运行 VS2008 Windows CE / Compact Framework 3.5 应用程序时:
Uri _baseAddress = new Uri("http://localhost:48614/");
string localFile = "fetchedVendorItems.txt";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_baseAddress + "api/vendoritems/");
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
// Retrieve response stream and wrap in StreamReader
Stream respStream = resp.GetResponseStream();
StreamReader rdr = new StreamReader(respStream);
// Create the local file
StreamWriter wrtr = new StreamWriter(localFile);
// loop through response stream reading each line and writing to the local file
string inLine = rdr.ReadLine();
while (inLine != null)
{
wrtr.WriteLine(inLine);
inLine = rdr.ReadLine();
}
rdr.Close();
wrtr.Close();
(我从这里改编:http: //msdn.microsoft.com/en-us/library/aa446517.aspx)
...我得到,“无法连接到远程服务器”
更新 2
这确实可以直接在开发机器上的浏览器中工作:
http://localhost:48614/api/redemptions/
它从控制器返回这些值:
readonly Redemption[] redemptions =
{
new Redemption { RedemptionId = "1", RedemptionName = "Old", RedemptionItemId = "ABC", RedemptionAmount = 0.25M, RedemptionDept = "2.0", RedemptionSubDept = "42" },
new Redemption { RedemptionId = "2", RedemptionName = "Damaged", RedemptionItemId = "BCD", RedemptionAmount = 5.00M, RedemptionDept = "42.0", RedemptionSubDept = "76" },
new Redemption { RedemptionId = "3", RedemptionName = "Rebate", RedemptionItemId = "DEF", RedemptionAmount = 42.75M, RedemptionDept = "76.0", RedemptionSubDept = "112" }
};
...像这样:
<ArrayOfRedemption xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/HHSServerWebAPI.Models">
<Redemption>
<RedemptionAmount>0.25</RedemptionAmount>
<RedemptionDept>2.0</RedemptionDept>
<RedemptionId>1</RedemptionId>
<RedemptionItemId>ABC</RedemptionItemId>
<RedemptionName>Old</RedemptionName>
<RedemptionSubDept>42</RedemptionSubDept>
</Redemption>
<Redemption>
<RedemptionAmount>5.00</RedemptionAmount>
<RedemptionDept>42.0</RedemptionDept>
<RedemptionId>2</RedemptionId>
<RedemptionItemId>BCD</RedemptionItemId>
<RedemptionName>Damaged</RedemptionName>
<RedemptionSubDept>76</RedemptionSubDept>
</Redemption>
<Redemption>
<RedemptionAmount>42.75</RedemptionAmount>
<RedemptionDept>76.0</RedemptionDept>
<RedemptionId>3</RedemptionId>
<RedemptionItemId>DEF</RedemptionItemId>
<RedemptionName>Rebate</RedemptionName>
<RedemptionSubDept>112</RedemptionSubDept>
</Redemption>
</ArrayOfRedemption>
...即使 VS2008 项目没有运行 - 是因为这个数据被缓存了(我第一次进入:
http://localhost:48614/api/redemptions/
...在浏览器中,Web API 应用程序正在运行)?
我知道模拟器不会将“localhost”识别为桌面实例,认为自己是某人/其他地方。那么如何在模拟器上进行测试呢?我可以使用什么 IP 地址?