简体   繁体   中英

HttpClient with Unity / Hololens

I'm currently playing around with Hololens and Hololens-programming. For my task I need to communicate with my REST API and build some stuff with the data.

I'm trying to use the HttpClient for my task.

I installed it with Install-Package Microsoft.AspNet.WebApi.Client , so it works, when I'm in Visual Studio 2017 for editing the scripts. The Namespace System.Net.Http works fine.

But when I switch to Unity, it keeps telling me the following:

Assets/Scripts/RestClient.cs(77,46): error CS1061: Type 'System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method 'ReadAsAsync' of type 'System.Net.Http.HttpContent' could be found. Are you missing an assembly reference?

It references this code snippet from the microsoft documentation I linked before:

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

It simply says, there is no ReadAsAsync<T>() , but it works in Visual Studio 2017 and it's in the docu. I'm so confused right know.

I'm trying to solve this already almost the whole day.

Already did: this

Mark your code with the platform #define directive NETFX_CORE . Example:

#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 platform #define directives : https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Also, make sure you have the InternetClient capability checked in Player Settings/Publishing Settings/Capabilities to send your messages over Internet. If you intend to work on a local network, you will need the PrivateNetworkClientServer capability instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM