从Xamarin Portable Class Library发送Http请求 [英] Sending Http requests from Xamarin Portable Class Library

查看:215
本文介绍了从Xamarin Portable Class Library发送Http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个非常简单的任务中遇到了相当不错的问题,并寻求帮助。
问题是:Xamarin PCL使用超时执行Http请求的最佳和最可靠的方法是什么?

I have quite decent problems with this very simple task and asking for help. The question is: what is the best and the most reliable way to perform Http requests with timeout from Xamarin PCL?

有几种选择:


  1. 使用旧的HttpWebRequest。这个解决方案肯定会起作用,但我在这个问题中描述的BeginGetRequestStream方法有问题: HttpWebRequest从未调用过BeginGetRequestStream回调它也是需要一些额外的丑陋代码来正确实现超时。

  1. Using old HttpWebRequest. This solution should definitely be working, but I have problems with BeginGetRequestStream method described in this question: HttpWebRequest BeginGetRequestStream callback never called Also it requres some extra ugly code to implement timeouts properly.

HttpClient。 PCL开箱即用的HttpClient不可用(我正在使用MvvmCross库所需的Profile 158)。我试图安装Microsoft HTTP客户端库但我仍然无法编译PCL项目,错误是:

HttpClient. HttpClient is not available in PCL out-of-the-box (I am using Profile 158 required by MvvmCross libraries). I was trying to install Microsoft HTTP Client Libraries but I still cannot compile the PCL project, error is:


类型或命名空间名称'Http 命名空间'System.Net'中不存在(你是否缺少程序集引用?)

The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)


  • PortableRest NuGet包。同样的问题 - 似乎在PCL中不起作用。大量的编译错误和警告,例如:

  • PortableRest NuGet package. Same problem - does not seem to work in PCL. Lots of compilation errors and warnings, for example:


    无法解析主要参考System.Net.Http,因为它有一个间接依赖于框架程序集System.Runtime,Version = 1.5.11.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a,无法在当前目标框架中解析。 .NETPortable,版本= V4.0,外形= Profile158\" 。要解决此问题,请删除引用System.Net.Http或将应用程序重新定位到包含System.Runtime,Version = 1.5.11.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a的框架版本。 (MSB3268)(Prototype.Core)

    The primary reference "System.Net.Http" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETPortable,Version=v4.0,Profile=Profile158". To resolve this problem, either remove the reference "System.Net.Http" or retarget your application to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". (MSB3268) (Prototype.Core)


  • 我做错了什么?为什么我无法使用这些基本标准库?

    What am I doing wrong? Why am I not able to use such basic standard libraries?

    更新

    最后通过切换到Profile 78(.NET 4.5)解决了这个问题。现在我可以使用HttpClient,它更好,更容易使用。它可以满足我的所有要求。 MvvmCross库也适用于Profile 78。

    Finally solved the issue by switching to Profile 78 (.NET 4.5). Now I can use HttpClient which is way better and easy to use. It works reliably with all my requests. MvvmCross libraries work fine with Profile 78 as well.

    我唯一的问题是没有Timer类。也许我应该自己使用Task.Delay实现它。

    The only issue I have is lack of Timer class. Maybe I should implement it myself using Task.Delay.

    推荐答案

    我建议通过NuGet使用HttpClient包 http://www.nuget.org/packages/Microsoft.Net.Http/

    I recommend using the HttpClient package via NuGet http://www.nuget.org/packages/Microsoft.Net.Http/

    这适用于配置文件78和MvvmCross。您还可以获得异步/等待支持。

    This works with profile 78 and MvvmCross. You also get async/await support.

    这篇关于从Xamarin Portable Class Library发送Http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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