Search Unity

Hololens and Rest Calls "Get" "Post"

Discussion in 'VR' started by DragonValor, Sep 19, 2016.

  1. DragonValor

    DragonValor

    Joined:
    Jul 27, 2014
    Posts:
    7
    Hi together,
    I want to use some rest calls from the hololens but which namespace will work? The "GET" call will only give me a json list. The next point will be to split the informations... Is there an easy way?


    This code give me the right answer in the string result but during the build process for the hololens an error occures.

    Code (CSharp):
    1. Assets\Scripts\ControllerGetScript2.cs(31,51): error CS1061: 'HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'HttpWebRequest' could be found (are you missing a using directive or an assembly reference?)`
    2.  
    Can´t I use the httpwebrequest in the hololens?


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Net;
    5. using System.IO;
    6.  
    7. public class ControllerGetScript2 : MonoBehaviour {
    8.  
    9.    // Use this for initialization
    10.    void Start ()
    11.   {
    12.   Get("http://192.168.10.11/");
    13.    }
    14.  
    15.  
    16.   public string Get(string url)
    17.   {
    18.   string result = null;
    19.   Debug.Log(result);
    20.   HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    21.   using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    22.   {
    23.   StreamReader reader = new StreamReader(response.GetResponseStream());
    24.   result = reader.ReadToEnd();
    25.   }
    26.   Debug.Log(result);
    27.   return result;
    28.    
    29.   }
    30. }
    31.  
    THX Pad
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    That method is not available on .NET scripting backend. You'll either have to switch to IL2CPP, or use "GetResponseAsync" under an ifdef.
     
  3. DragonValor

    DragonValor

    Joined:
    Jul 27, 2014
    Posts:
    7
    HI, I read a lot about the GetResponseAsync Method but it´s implemented in .Net 4 oder 4.5 and the hololens Unity version only understand 3.5. Is there a solution for that?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    If you're using .NET scripting backend, you get access to .NET Core 5 API. You just need to surround your code with the define:

    Code (csharp):
    1. #if NETFX_CORE
    2. // code that uses .NET Core APIs
    3. #endif
    That code will not run in the editor.
     
  5. DragonValor

    DragonValor

    Joined:
    Jul 27, 2014
    Posts:
    7
    Right now I get the class UnityWebRequest. This also works for me and it is realy easy.