HttpClient with Unity / Hololens



我目前正在与HoloLens和HoloLens编程一起玩。对于我的任务,我需要与我的REST API进行通信并与数据构建一些内容。

我正在尝试将httpclient用于我的任务。

我使用Install-Package Microsoft.AspNet.WebApi.Client安装了它,因此当我在Visual Studio 2017中进行编辑脚本时,它可以正常工作。名称空间System.Net.Http工作正常。

但是,当我切换到Unity时,它会一直告诉我以下内容:

资产/脚本/restclient.cs(77,46):错误CS1061:类型 'System.net.http.httpcontent'不包含针对的定义 " readasasync'和无扩展方法'readasasync'类型 可以找到'System.net.http.httpcontent'。你错过了 组装参考?

它从我之前链接的Microsoft文档中引用此代码段:

static async Task<Product> GetProductAsync(string path)
{
    Product product = null;
    HttpResponseMessage response = await client.GetAsync(path);
    if (response.IsSuccessStatusCode)
    {
        product = await response.Content.ReadAsAsync<Product>();
    }
    return product;
}

简单地说,没有ReadAsAsync<T>(),但它在Visual Studio 2017中起作用,并且在Docu中。我很困惑地知道。

我正在尝试几乎整天解决这个问题。

已经做到了:这个

用平台#Define指令 netfx_core 标记您的代码。示例:

#if !NETFX_CORE
    Debug.LogError("API is restricted to Universal Windows Platform"); // Error in Editor and elsewhere but not on UWP i.e. Hololens
#else
    // your UWP specific code to run on Hololens
#endif

Unity平台#Define指令:https://docs.unity3d.com/manual/platformdependententcompilation.html

另外,请确保您拥有 Internetclient 在玩家设置/发布设置/功能中检查功能,以通过Internet发送您的消息。如果您打算在本地网络上工作,则需要 PrivateNetworkClientserver 功能。

最新更新