Search Unity

Question Httprequest works in Unity Editor but does not work on Hololens

Discussion in 'AR' started by ariansyah, Jun 11, 2021.

Thread Status:
Not open for further replies.
  1. ariansyah

    ariansyah

    Joined:
    Dec 9, 2014
    Posts:
    6
    Hi,

    I use System.net HttpWebRequest and HttpWebResponse to create GET and POST request. When I run my app in Unity Editor, everything works well. The code can read the data from the server.

    However, when I deploy on Hololens, the code fails to read the data from the server and throw exception message: "connectfailure (value does not fall within the expected range.)" when I debugged the code running on Hololens.

    My system:

    Unity 2019.1.10f1
    Visual studio community 2017 version 15.9.34
    InternetClient, InternetClientServer, and PrivateNetworkClientServer are all enabled

    Build targeted SDK version 10.0.18362.0

    and here is the code


    Code (CSharp):
    1.  
    2.  
    3.     using System.Collections;
    4.     using System.Collections.Generic;
    5.     using UnityEngine;
    6.     using System.Net.Http;
    7.     using System.Net;
    8.     using System.IO;
    9.     using System;
    10.    
    11.     public class makeRequest
    12.     {
    13.         public enum TypeOfRequest { GET, POST }
    14.    
    15.         public static string HTTPRequest(string url, string json, TypeOfRequest req)
    16.         {
    17.             string strResponseValue = string.Empty;
    18.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    19.    
    20.             request.ContentType = "application/json";
    21.             request.Method = req.ToString();
    22.    
    23.    
    24.             if (json != null && request.Method == "POST")
    25.             {
    26.    
    27.                 using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
    28.                 {
    29.                     sw.Write(json);
    30.                     sw.Close();
    31.                 }
    32.             }
    33.    
    34.    
    35.    
    36.             HttpWebResponse response = null;
    37.             try
    38.             {
    39.                 response = (HttpWebResponse)request.GetResponse();
    40.    
    41.                 using (Stream responseStream = response.GetResponseStream())
    42.                 {
    43.                     if (responseStream != null)
    44.                     {
    45.                         using (StreamReader reader = new StreamReader(responseStream))
    46.                         {
    47.                             strResponseValue = reader.ReadToEnd();
    48.                         }
    49.                     }
    50.                 }
    51.             }
    52.    
    53.             catch (Exception ex)
    54.             {
    55.    
    56.                 strResponseValue = "{\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
    57.             }
    58.             finally
    59.             {
    60.                 if (response != null)
    61.                 {
    62.                     ((IDisposable)response).Dispose();
    63.                 }
    64.             }
    65.    
    66.    
    67.             return strResponseValue;
    68.         }
    69.     }
    70.  
    71.  
    72.  

    Any idea how to fix this?

    Thanks
     
  2. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
Thread Status:
Not open for further replies.