RFC上的HttpWebRequest VS RESTSharp从Windows CE / Compact Framework的3.5 [英] RFC on HttpWebRequest vs RESTSharp from Windows CE / Compact Framework 3.5

查看:381
本文介绍了RFC上的HttpWebRequest VS RESTSharp从Windows CE / Compact Framework的3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个.NET4简单的WebAPI服务和Visual Studio 2010。

I created a simple WebAPI service in .NET4 and Visual Studio 2010.

我需要消耗该服务在Windows CE / CF 3.5应用

I need to consume that service in a Windows CE / CF 3.5 app

我有HttpWebRequest的提供给我,但我不知道这是要走的路,或者我应该使用RestSharp。是否有人有洞察力/经验,这将有助于我作出决定?我preFER不使用第三方code时,尽量避免它,但我愿意这样做,如果有过原始的产品具有明显的优势。

I do have HttpWebRequest available to me, but am not sure if this is the way to go, or I should use RestSharp. Does anybody have insight/experience that would help me decide? I prefer not using 3rd party code when possible to avoid it, but am willing to do so if there is a clear advantage over the "raw" offerings.

有没有人有,还是知道的,从CF 3.5使用访问HttpWebRequest的OR RestSharp的WebAPI服务的例子吗?

Does anyone have, or know of, examples for accessing WebApi services using HttpWebRequest OR RestSharp from CF 3.5?

我有这样的code(改编从<一个href=\"http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api\" rel=\"nofollow\">http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api)样品的WebAPI方式:

I have this code (adapted from from http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api) for sample WebAPI methods:

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的消耗这一点。

...but don't know how to consume this using, if possible, HttpWebRequest.

注:HttpClient的不提供给我(HttpWebRequest的是,虽然)

Note: HttpClient is not available to me (HttpWebRequest is, though).

我开始具有的WebAPI方法VS2010应用;但是当我运行VS2008的Windows CE /紧凑型Framekwork 3.5的应用程序与此code:

I start the VS2010 app that has the WebAPI method; but when I run the VS2008 Windows CE / Compact Framekwork 3.5 app with this code:

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

...我得到无法连接到远程服务器

...I get, "Unable to connect to the remote server"

此的确实的直接在开发计算机上的浏览器工作:

This does work directly in the browser on the dev machine:

http://localhost:48614/api/redemptions/

据从控制器返回这些值:

It returns these values from a Controller:

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" }
};

......像这样:

...like so:

<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的项目没有运行 - 是,因为这个数据缓存(我第一次进入:

...even when the VS2008 project is not running - is that because this data was cached (the first time I entered:

http://localhost:48614/api/redemptions/

...在浏览器中,在Web API应用程序的的运行)?

我得到一个模拟器无法识别localhost作为桌面实例,考虑到自己一个人/别处。所以,我怎么能在模拟器上测试这个?我可以使用什么IP地址?

I get that the emulator won't recognize "localhost" as the desktop instance, considering itself someone/somewhere else. So how can I test this on an emulator? What IP address can I use?

推荐答案

避免第三方code不可收拾实在是太傻。为什么要推倒重来?如果你是一个公司谁的IP正在RES​​T调用,那么肯定的是,滚吧,但我怀疑你的核心业务产品是解决其他一些问题。我的意思是,为什么使用CF本身,而不是C?为什么要使用C和汇编不?为什么要使用第三方处理器,而不是设计自己的?

Avoiding 3rd party code out of hand is just plain silly. Why reinvent the wheel? If you are a company who's IP is making REST calls, then sure, roll it, but I suspect your core business offering is in solving some other problem. I mean why use the CF itself, and not C? Why use C and not assembly? Why use a third-party processor and not design your own?

所有这一切不谈,RestSharp自带光源,它是免费的,所以有在使用它的风险很小。有一些东西我喜欢它 - 主要是,大多数为REST调用繁重的工作来完成。我不是重塑东西的大风扇。它有一些怪癖,我已经固定在本地(我的意思做了一个拉要求,只是还没有找到的时候还没有),但他们是未成年人,而不是什么我会认为是典型案例。

All that aside, RestSharp comes with source and it's free, so there's little risk in using it. There are some things I like about it - primarily that most of the grunt work for REST calls done. I'm a big fan of not reinventing things. It has some quirks that I've "fixed" locally (I'm meaning to do a pull request, just haven't found the time yet) but they were minor and not what I'd consider to be "typical" cases.

至于与调用的Web RestSharp的API,有一个的 pretty全面覆盖了在这篇文章

As for calling Web APIs with RestSharp, there's a pretty thorough coverage over in this article.

这篇关于RFC上的HttpWebRequest VS RESTSharp从Windows CE / Compact Framework的3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆