我创建了一个.NET4简单的WebAPI服务和Visual Studio 2010。
我需要消耗在Windows CE系统服务/ CF 3.5应用程序
我有HttpWebRequest的提供给我,但我不知道这是要走的路,或者我应该使用RestSharp。 是否有人有洞察力/经验,这将有助于我的决定? 我不喜欢使用第三方代码时可能避免它,但我愿意这样做,如果有过“原始”的产品具有明显的优势。
任何人都不会有,或知道的,对于使用HttpWebRequest的OR RestSharp从CF 3.5访问的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的是,虽然)。
UPDATE
我开始具有的WebAPI方法的VS2010应用; 但是当我运行VS2008的Windows CE /紧凑型Framekwork 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地址?