Exception thrown when setting up HttpClientHandler

Hi all,

The problem is an exception being thrown when setting up a HttpClientHandler for communication with a REST API.

I have a Unity application with two bespoke DLLs. The application needs to interface to external systems, so one bespoke DLL provides the interface to the external systems and the other supplies common functionality.

The external system in question is a REST API, so when required by the application, the DLL creates a HttpClient, HttpRequestMessage etc, sends out the request, receives back JSON and hands back the data to the application.

The problem occurs when setting up comms with the external system. The code below is being executed:

class RestApiAccess
{
    private static HttpClient httpClient;
 
    public RestApiAccess()
    {
        ICredentials credentials = CredentialCache.DefaultCredentials;

        HttpClientHandler handler = new HttpClientHandler();
        handler.DefaultProxyCredentials = credentials;

        httpClient = new HttpClient(handler);
    }
}

There’s an exception thrown on the assignment to handler.DefaultProxyCredentials (line 10 above); “the method or operation is not implemented”.

Assuming that this means DefaultProxyCredentials isn’t implemented, this implies that there’s some sort of version mismatch in .NET DLLs, since DefaultProxyCredentials was introduced in .NET Framework 4.7.1.

I have verified that the Visual Studio projects are all targeting .NET Framework 4.7.1. I’ve also added “-r:System.Net.Http.dll” to the csc.rsp file.

The code has also been tested while targeting .NET Framework 4.7.2 and 4.8, with the same results.

Testing the DLL with a separate test harness (i.e. not Unity) and it works as expected. It’s just the Unity application that doesn’t work with the DLL.

I’m at a bit of a loss now; has anyone seen anything like this before?

Windows 10
Unity 2019.3.0f6
Visual Studio Professional 2019 version 16.7.0
.NET Framework 4.8.03761, targeting 4.7.1

Thanks!

Can you provide the full call stack for the exception? Also, does this happen when the code is running in the Unity Editor or in a player build? If a player build, which platform and scripting backend is the player built for?

Hi Josh, we put in a support email (#905535) and attached a zipped project with the error occurring.

This happens in Editor and in a build.

The stack trace is not much help:

STACK TRACE = at System.Net.Http.HttpClientHandler.set_DefaultProxyCredentials (System.Net.ICredentials value) [0x00000] in <7ebf3529ba0e4558a5fa1bc982aa8605>:0

at HttpClientTest.RunTesting () [0x00073] in C:.…\Assets\Scenes\HttpClientTest.cs:27

Ok, thanks! A support person should be contacting you soon then.

Hi Josh, we were told that the support was for admin issues only, not technical, and that this forum was the place to get technical help. Please see the submitted ticket for reference to the project.

I’m unsure what that ticket number corresponds to. It does not seem to be a bug report though. Can you submit a bug report via this process? https://unity3d.com/unity/qa/bug-reporting

Hi Josh, I have - case Case 1278647. I have also been told via support email that Unity runs with .NET 4.6! This would explain our issue, however it does not explain why Unity uses an older version of the .NET Framework. Are there plans to update this?

Unity uses a version of the .NET class libraries that is API-compatible with .NET 4.7.1, but some parts of the API are not implemented. Unfortunately, HttpClient.DefaultProxyCredentials is one of those parts of the API that is not implemented now.

We’re working on updates to the class libraries at the moment, but I don’t expect this to be shipped in a version of Unity for a few months at least. So unfortunately, this is not something we can implement now in Unity.

Thanks, I’ll keep my fingers crossed for a 2020.2 release.

Thanks for the information, Josh. How would you suggest that we go about providing the default proxy credentials? Our application won’t work without them.

Ok, we have targeted our project at .NET Framework 4.6, but we are still unable to pass the proxy credentials successfully. The code works outside of the Unity project, it’s only when Unity runs it that an exception is thrown.

The exception is “Proxy Authentication Required”.

private static string proxyTemp = "proxy:port";

private static string baseUrl = "https://jsonplaceholder.typicode.com/";

private static string extendUrl = "users/1";

static private async Task TestWebProxy()
{
    try
    {
        HttpClientHandler handler = new HttpClientHandler
        {
            Proxy = new WebProxy(proxyTemp, true, null, CredentialCache.DefaultNetworkCredentials)
        };

        HttpClient httpClient = new HttpClient(handler);

        httpClient.BaseAddress = new Uri(baseUrl);

        HttpResponseMessage response = await httpClient.GetAsync(extendUrl);

        response.EnsureSuccessStatusCode();

        var statusText = "done\n" + response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;
        Console.WriteLine(statusText);
        Console.ReadKey();
    }
    catch(Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadKey();
    }
}