我已经使用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应用启用EnableLoopback。
我的问题是:要获得一个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);
}
}
}
}
}
团结已经承认这是一个问题:
https://fogbugz.unity3d.com/default.asp?1222589_u6qsndet3umnp50u
https://issuetracker.unity3d.com/issues/httpclient-ignores-windows-proxy
似乎在“ IL2CPP”代码生成过程中,生成的代码将HttpClient更改为not使用本地代理。
下面的代码是从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);