[SOLVED] HttpUtility in IL2CPP

Cant seem to use System.Web.HttpUtility in IL2CPP builds. Works fine in Mono builds.

Why is it missing from IL2CPP?

Example:

using System;
using System.Collections.Generic;
using System.Reflection;
using NUnit.Framework;

[TestFixture]
public class HttpUtilityTest
{
    [Test]
    public void HttpUtilityHtmlDecodeWorks()
    {
        var x = System.Web.HttpUtility.HtmlDecode("foo bar");
        Assert.AreEqual("foo bar", x);
    }

    [Test]
    public void HttpUtilityUrlDecodeWorks()
    {
        var x = System.Web.HttpUtility.UrlDecode("foo%20bar");
        Assert.AreEqual("foo bar", x);
    }
}

Error message (when testing build by IL2CPP on Windows):

HttpUtilityHtmlDecodeWorks (0,077s)
---
System.Exception : Error: called non-existent method System.String System.Web.HttpUtility::HtmlDecode(System.String)

HttpUtilityUrlDecodeWorks (0,080s)
---
System.Exception : Error: called non-existent method System.String System.Web.HttpUtility::UrlDecode(System.String)

Configuration:


Edit: Sidenote is that I did have a csc.rsp file containing:

-r:System.Web.dll

That might be of why? Does IL2CPP maybe not correctly honor csc.rsp files? (just thinking out loud here)

Using Unity 2019.2.11f1 btw.

You do need the csc.rsp file to reference System.Web.dll. That provides arguments to the C# compiler, which happens before IL2CPP runs.

However, the HtmlDecode and UrlDecode methods are not provided in the implementation of the class libraries that IL2CPP uses, mainly to decrease size. These might be methods that we could consider adding to the class library implementation for IL2CPP though, as it looks like they can be easily removed by the managed code linker.

In the mean time, you might be able to implement them yourself. The code for the class libraries is available here: mono/mcs/class/System.Web/System.Web/HttpUtility.cs at unity-main · Unity-Technologies/mono · GitHub

Alright! Yea just forking Mono’s implementation is what I ended up doing anyway. Thanks for reply!

Thank you for that answer!

But guys @ , are you serious?
We’re creating professional software with Unity, using the IL2CPP compiler for various reasons. After an hour of searching for a solution, several link.xml adjustments and long lasting builds, I find this post somewhere hidden in the internet, which provides your more or less official Unity solution saying:
“Sorry, we’ve silently not implemented these functions because you know, size. Go ahead and write them yourselves. Here’s a link.”

With all due respect,
I do hate you.

Update:
I actually didn’t need UrlDecode, but UrlEncode. This is not really possible to fork from the Mono repo because of all the referenced classes.
I’m trying to go with WebUtility now.

1 Like

Haha harsh comment. Know the pain though.

@JoshPeterson Is it possible you (or some other Unity employee) could extract a list of types that are not included in IL2CPP builds? So we could make a “polyfill”-esque package to resolve these kinds of problems?

You are right, but I (and everyone else) have a right to comment this way - with a wink of course.
It took another 3 hours to realize that IL2CPP for some reason can’t verify signed XMLs. Result is always “false”, while in Editor and Mono-build, everything works as expected.
Without a proper documentation for all the “Fancy stuff we’ve removed in IL2CPP to ruin your day”, this is just another pain in the… yeah.
I’m 3 weeks before the release to a customer.

I’m unaware of this issue. Can you elaborate? Maybe this is something we can correct. It may not be something that has been explicitly removed, but it might be an actual bug.

I think this is possible. At the moment, the “documentation” for APIs that don’t exist is that runtime error you get when calling them that you mentioned above. What type of documentation would be better. Would flat list of API methods be better?

I’m not sure what you mean here. What is "“polyfill”-esque?

u should avoid using web or rest api in unity, it was not meant for that purpose. there are many restful techs out there than can create much better experience if u expose relevant unity parts that need it.

Oh yea that one. I actually found it before and reported it. Here’s the case id: 1131451

Source: https://answers.unity.com/questions/1606257/securityexception-on-embedded-resx-when-using-il2c.html

Yes just a list of methods like “these types doesn’t work in IL2CPP builds, because reasons”.

And for polyfill package, I mean like a package that adds those methods back for IL2CPP, similar to just adding the script you mentioned from the mono source code of HttpUtility.cs, but something similar for most missing methods of the IL2CPP builds

Hi,

sincerely: Unity has been targeting AEC industries for years now. You’re far away from being just a gaming engine.
Your target audience is used to and relies on strong technical documentation, e.g.: A 100 pages long errata sheet for microcontrollers. These contain for example all known errors and things that are not working as intended, including best practices and work-arounds.

With respect to IL2CPP:
Your target AEC audience WILL use everything that the .NET framework provides, under the sole restricition of its availability in the Mono framework. This includes also encryption and other stuff that is usually not used for games.

If something is working differently in Unity compared to blank Mono, this should be clearly mentioned, including work-arounds and all necessary background information, on a clear and easy to find web portal or some kind of document.

Having said that:
No, a flat list of API methods is actually not sufficient for engineers that use Unity as intended.
I so much want to use Unity with IL2CPP for a our Software. But what’s holding us back is the “trial and error” engineering that is necessary to cover all unknown incompatibilites.

1 Like