Search Unity

Issues with HttpWebRequest on IL2CPP for UWP

Discussion in 'Windows' started by ScrappyOrc, May 30, 2017.

  1. ScrappyOrc

    ScrappyOrc

    Joined:
    Nov 25, 2013
    Posts:
    14
    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: https://forum.unity3d.com/threads/log-spam-0x000006a6-the-binding-handle-is-invalid.238449/ , 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:
    Code (C++):
    1. WaitStatus SocketImpl::GetHostByName(const std::string &host, std::string &name, std::vector<std::string> &aliases, std::vector<std::string> &addr_list)
    2. {
    3.     char this_hostname[256] = {0};
    4.  
    5.     const char *hostname = host.c_str();
    6.     bool add_local_ips = (*hostname == '\0');
    7.  
    8.     if (!add_local_ips && gethostname(this_hostname, sizeof(this_hostname)) != -1)
    9.     {
    10.         if (!strcmp(hostname, this_hostname))
    11.             add_local_ips = true;
    12.     }
    13.  
    14.     struct hostent *he = NULL;
    15.     if (*hostname)
    16.         he = gethostbyname(hostname);
    17.  
    18.     if (*hostname && he == NULL)
    19.         return kWaitStatusFailure;
    20.  
    21.     return (add_local_ips
    22.             ? hostent_get_info_with_local_ips(he, name, aliases, addr_list)
    23.             : hostent_get_info(he, name, aliases, addr_list))
    24.         ? kWaitStatusSuccess
    25.         : kWaitStatusFailure;
    26. }
     
    Last edited: May 30, 2017
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    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:

    Code (csharp):
    1. try
    2. {
    3.     throw 2;
    4. }
    5. catch (...)
    6. {
    7. }
    would cause the same issue, even though it's harmless (the exception is caught).
     
  3. ScrappyOrc

    ScrappyOrc

    Joined:
    Nov 25, 2013
    Posts:
    14
    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.
     
  4. ScrappyOrc

    ScrappyOrc

    Joined:
    Nov 25, 2013
    Posts:
    14
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Net;
    5. using System.IO;
    6. using System.Text;
    7. using System;
    8.  
    9. public class WebReqTester : MonoBehaviour
    10. {
    11.  
    12.     private string txt = "";
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         HttpWebRequest request = (HttpWebRequest)global::System.Net.WebRequest.Create("http://www.google.com/");
    18.         request.Method = "GET";
    19.         WebResponse response = null;
    20.  
    21.         try
    22.         {
    23.             Debug.Log("Performing GetResponse()");
    24.             response = request.GetResponse();
    25.         }
    26.         catch (Exception e)
    27.         {
    28.             Debug.LogFormat("Error with GetResponse:\n{0}", e);
    29.             response = null;
    30.         }
    31.  
    32.         if (response != null)
    33.         {
    34.             Stream stream = response.GetResponseStream();
    35.             StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("UTF-8"));
    36.             txt = sr.ReadToEnd();
    37.             sr.Close();
    38.             stream.Close();
    39.         }
    40.         Debug.LogFormat("Response from server:\n{0}", txt);
    41.     }
    42. }
    43.  
    This works fine in editor, but when built with IL2CPP for UWP, the an exception gets printed out:
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    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.
     
  6. ScrappyOrc

    ScrappyOrc

    Joined:
    Nov 25, 2013
    Posts:
    14
    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!