Search Unity

HttpClient on .Net Standard 2.0

Discussion in 'Scripting' started by LievenVdA, Jan 7, 2019.

  1. LievenVdA

    LievenVdA

    Joined:
    Oct 1, 2018
    Posts:
    3
    Hi there,

    I'm working on a library for Unity where we can use the System.Net.Http.HttpClient in Unity targeting .Net Standard 2.0.
    This works perfectly in a test project in Visual Studio, separate from Unity, but when used in Unity, the following code throws an exception:
    Code (CSharp):
    1.         public static HttpClient Create(TimeSpan timeout = default(TimeSpan), ICredentials credentials = null, string proxy = null)
    2.         {
    3.             var useProxy = !string.IsNullOrEmpty(proxy);
    4.             var config = new HttpClientHandler
    5.             {
    6.                 UseProxy = useProxy,
    7.                 Proxy = useProxy ? new WebProxy(proxy) : null
    8.             };
    9.             if (useProxy)
    10.                 config.DefaultProxyCredentials = credentials;
    11.             if (credentials != null)
    12.             {
    13.                 config.UseDefaultCredentials = false;
    14.                 config.Credentials = credentials;
    15.             }
    16.             var client = new HttpClient(config);
    17.  
    18.             if (!timeout.Equals(default(TimeSpan)))
    19.                 client.Timeout = timeout;
    20.             return client;
    21.         }
    The error thrown is InvalidOperationException:
    Operation is not valid due to the current state of the object. at System.Net.Http.HttpClientHandler.set_Proxy (System.Net.IWebProxy value) [0x0000e] in <20a8f293e89843148f03a963627efba4>:0, ​
    this references line 7 in the code above.

    My question is: has anyone here encountered the same problem? Is the HttpClient class not fully supported when targeting .Net Standard 2.0? I'm at a loss here, the code compiles perfectly, but at runtime it seems it behaves quite differently in Unity vs. a normal .Net project.
     
  2. Deleted User

    Deleted User

    Guest

    It took me some time to get it right but httpClient is supported by Unity out of the box on .NET 2.0 standard targets. This is the code I ended up with to access a REST api:
    Code (CSharp):
    1.  
    2. public static async Task<Resource> GetResource()
    3.         {
    4.             using (var httpClient = new HttpClient())
    5.             {
    6.                 httpClient.BaseAddress = new Uri(URL);
    7.                 httpClient.DefaultRequestHeaders.Accept.Clear();
    8.                 httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    9.                 var response = await httpClient.GetAsync("api/session");
    10.                 if (response.StatusCode != HttpStatusCode.OK)
    11.                     return null;
    12.                 var resourceJson = await response.Content.ReadAsStringAsync();
    13.                 return JsonUtility.FromJson<Resource>(resourceJson);
    14.             }
    15.         }
    16.  
     
    Last edited by a moderator: Dec 13, 2019
    MurphyMurph_21 and xaldin-76 like this.