Issues with HttpWebRequest on IL2CPP for UWP

We’re using HttpWebRequest to log users into a server, and our login system is working fine on the other platforms we’re targeting: Standalone and iOS (IL2CPP). It’s hard to tell exactly where the error is occuring, but it seems to happen when we call webRequest.BeginGetRequestStream(asyncCallback, webRequest);

In the IL2CPP project, we’re getting the error: “Exception thrown at 0x7458B802 (KernelBase.dll) in MyGame.exe: 0x000006A6: The binding handle is invalid.” I found another thread here: Log Spam: 0x000006A6: The binding handle is invalid - Unity Engine - Unity Discussions , but it doesn’t seem like that solution works for our project.

When debugging the project, the exception gets thrown on the line “he = gethostbyname(hostname)” in this function:

WaitStatus SocketImpl::GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list)
{
    char this_hostname[256] = {0};

    const char *hostname = host.c_str();
    bool add_local_ips = (*hostname == '\0');

    if (!add_local_ips && gethostname(this_hostname, sizeof(this_hostname)) != -1)
    {
        if (!strcmp(hostname, this_hostname))
            add_local_ips = true;
    }

    struct hostent *he = NULL;
    if (*hostname)
        he = gethostbyname(hostname);

    if (*hostname && he == NULL)
        return kWaitStatusFailure;

    return (add_local_ips
            ? hostent_get_info_with_local_ips(he, name, aliases, addr_list)
            : hostent_get_info(he, name, aliases, addr_list))
        ? kWaitStatusSuccess
        : kWaitStatusFailure;
}

This looks like an internal first-chance exception that is happening inside “gethostbyname” function, which is part of windows networking API. When you get the exception, there should be a pop up with a checkbox saying “break when this exception type is thrown” (or something similar to that). Uncheck that checkbox, and click continue. It will not bother you again.

You might be wondering why this is happening. By default, Visual Studio is configured to break on any C++ exceptions, even if they’re caught. Something like this:

try
{
    throw 2;
}
catch (...)
{
}

would cause the same issue, even though it’s harmless (the exception is caught).

When I disable the “break when this exception type is thrown” the UWP app still isn’t receiving responses from the server. I’m hesitant to think that it’s an issue with the server or the requests because we’re able to log in on other platforms.

I made a small repro project, and am still seeing the error on UWP, but not on other platforms. I added this script to a game object in an empty scene:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.IO;
using System.Text;
using System;

public class WebReqTester : MonoBehaviour
{

    private string txt = "";

    // Use this for initialization
    void Start()
    {
        HttpWebRequest request = (HttpWebRequest)global::System.Net.WebRequest.Create("http://www.google.com/");
        request.Method = "GET";
        WebResponse response = null;

        try
        {
            Debug.Log("Performing GetResponse()");
            response = request.GetResponse();
        }
        catch (Exception e)
        {
            Debug.LogFormat("Error with GetResponse:\n{0}", e);
            response = null;
        }

        if (response != null)
        {
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("UTF-8"));
            txt = sr.ReadToEnd();
            sr.Close();
            stream.Close();
        }
        Debug.LogFormat("Response from server:\n{0}", txt);
    }
}

This works fine in editor, but when built with IL2CPP for UWP, the an exception gets printed out:

Did you enable networking capability in the manifest/player settings?

EDIT: your sample code seems to work here on 5.6.1f1. If I disable internet capability in the manifest, I get the same name resolution failure as you did.

After enabling the “Internet Client” in the player settings, it works and lets me log into the server in our app. Thanks for your help!