如何让 Fiddler 或 Charles 从 Unity 2018 IL2CPP 应用程序捕获流量

How to get Fiddler or Charles to capture traffic from Unity 2018 IL2CPP apps

我已经使用 Unity 5 和 Unity 2017 构建了应用程序,并且能够使用 Fiddler 或 Charles 成功捕获网络流量。但是,当我使用 IL2CPP 构建 Unity 2018 应用程序时(其他应用程序是使用 .Net 构建的),该应用程序可以正常工作(成功发送网络流量),但该应用程序似乎以某种方式绕过了 Fiddler(或 Charles)代理。也就是说,Fiddler 和 Charles 无法显示来自 Unity 2018 IL2CPP 应用的网络流量,即使它可以显示来自 Unity 5 和 Unity 2017 应用的流量。

请注意,一切都适用于 Unity 5 和 Unity 2017,并且之前已经设置了 SSL 设置等。此外,我已经使用 Fiddler "WinConfig" 为 Unity 2018 应用启用回环。

我的问题是:我需要做什么才能让 Unity 2018 IL2CPP 应用程序在 Fiddler 或 Charles 中显示流量?

更新: 下面是一些示例代码,显示 HttpClient 请求不通过代理,但其他 (Unity) web 请求确实通过代理:

public class RequestTest : MonoBehaviour
{
    public UnityEngine.UI.Text text;

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.P))
        {
            StartCoroutine(yieldPing());
        }
        if (Input.GetKeyUp(KeyCode.O))
        {
            asyncPing();
        }
    }

    private IEnumerator yieldPing()
    {
        Debug.Log("This request shows up in Fiddler");
        text.text = "UWR in Coroutine";
        using (UnityWebRequest uwr = UnityWebRequest.Get("https://www.google.com/"))
        {
            yield return uwr.SendWebRequest();
        }
    }

    private async void asyncPing()
    {
        await awaitPing();
    }

    private async System.Threading.Tasks.Task<bool> awaitPing()
    {
        Debug.Log("This request also shows up in Fiddler");
        UnityWebRequest uwr = UnityWebRequest.Get("https://www.google.com/");
        text.text = "UWR in async await";
        uwr.SendWebRequest().completed += delegate
        {
            uwr.Dispose();
        };

        Debug.Log("This request does NOT show up in Fiddler???");
        text.text += "\nHttpClient in async await";
        using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient())
        {
            using (System.Net.Http.HttpRequestMessage httpRequest = new System.Net.Http.HttpRequestMessage())
            {
                httpRequest.RequestUri = new System.Uri("http://www.youtube.com/");
                httpRequest.Method = System.Net.Http.HttpMethod.Get;
                using (System.Net.Http.HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest, System.Net.Http.HttpCompletionOption.ResponseHeadersRead))
                {
                    var responseCode = (int)httpResponse.StatusCode;
                    // We will get a 304 if the content has not been modified
                    // If there is new, good content, then we will get a 200
                    return (responseCode == 200);
                }
            }
        }
    }
}

Unity 播放器设置如下:

Unity 已确认这是一个问题:

https://fogbugz.unity3d.com/default.asp?1222589_u6qsndet3umnp50u

https://issuetracker.unity3d.com/issues/httpclient-ignores-windows-proxy

似乎在 "IL2CPP" 代码生成过程中,生成的代码将 HttpClient 更改为 使用本地代理。

下面的代码是从 here 复制的,似乎克服了 Unity IL2CPP 错误:

 // might be overly complicated, there is an option to wrap the existing proxy here...
class myWebProxy : System.Net.IWebProxy
{
    private System.Net.IWebProxy wrappedProxy;
    private System.Net.ICredentials creds;
    private void init()
    {
        wrappedProxy = null;
        creds = CredentialCache.DefaultCredentials;
    }
    public myWebProxy()
    {
        init();
    }

    public myWebProxy(System.Net.IWebProxy theWrappedProxy)
    {
        init();
        wrappedProxy = theWrappedProxy;
    }
    public System.Net.ICredentials Credentials
    {
        get
        {
            if (wrappedProxy != null)
            {
                return wrappedProxy.Credentials;
            }
            else
            {
                return creds;
            }
        }
        set
        {
            if (wrappedProxy != null)
            {
                wrappedProxy.Credentials = value;
            }
            else
            {
                creds = value;
            }

        }
    }

    public Uri GetProxy(Uri destination)
    {
        if (wrappedProxy != null /* todo or Uri == certain Uri */)
        {
            return wrappedProxy.GetProxy(destination);
        }
        else
        {
            // hardcoded proxy here..
            return new Uri("http://seeplusplus:8080");
        }
    }

    public bool IsBypassed(Uri host)
    {
        if (wrappedProxy != null)
        {
            return wrappedProxy.IsBypassed(host);
        }
        else
        {
            return false;
        }

    }
}


// in your code use your new proxy
            HttpClientHandler aHandler = new HttpClientHandler();
// use your proxy!
            aHandler.Proxy = new myWebProxy();
            HttpClient client = new HttpClient(aHandler);