简体   繁体   English

WebRequest(System.Net)是Unity中的安全选择吗?

[英]Is WebRequest (System.Net) a safe choice in Unity?

First of all, let me state my problem: my game server does not provide WebAPI (we do not have resources for it now), but rather our client is tring to work like a web browser and I need cookie support for Session ID. 首先,让我陈述一下我的问题:我的游戏服务器不提供WebAPI(我们现在没有资源),但是我们的客户端正像Web浏览器一样工作,并且我需要对Session ID的cookie支持。

Searching around with Google, I see the best I can do is manually set the headers of request and get the response header. 通过Google搜索,我发现最好的方法是手动设置请求标头并获取响应标头。 I am ok with that, because I am originally ASP.NET MVC developer. 我可以接受,因为我最初是ASP.NET MVC开发人员。

However, I then realize they use Dictionary for both the request and response. 但是,然后我意识到他们将Dictionary用于请求和响应。 Now that's the problem. 现在就是问题了。 We know that the header can be duplicated, in my case is the Set-Cookie. 我们知道标题可以重复,在我的例子中是Set-Cookie。

Then I tried another, and find out UnityWebRequest class, which is still in UnityEngine.Experimental.Networking namespace (so I suppose it is still in beta?), but I try my luck anyway; 然后,我尝试了另一个,并找到了UnityWebRequest类,该类仍在UnityEngine.Experimental.Networking命名空间中(所以我想它仍处于beta中?),但是无论如何,我还是尝试了运气。 only sad to realize they also use Dictionary for header items. 只是悲哀地意识到,他们也使用词典的标题项目。

So now my only chance is the vanilla .NET WebRequest (in System.Net namespace). 因此,现在我唯一的机会是香草.NET WebRequest (在System.Net名称空间中)。 However, I see no documentation on the .NET Framework compability in Unity. 但是,我看不到有关Unity中.NET Framework兼容性的文档。 Can anyone tell me if it is supported on most platform? 谁能告诉我大多数平台是否支持它? My main targets are Windows, Android and Web. 我的主要目标是Windows,Android和Web。 If possible, even for WebClient would be nicer. 如果可能的话,即使对于WebClient也会更好。

Here is my current solution, which work good in the Unity Editor, but I have yet to test them on other devices. 这是我当前的解决方案,在Unity编辑器中可以很好地工作,但是我尚未在其他设备上对其进行测试。 Is there any solution for this? 有什么解决办法吗?

public class CookieWebRequest
{

    private CookieContainer cookieContainer;


    public CookieWebRequest()
    {
        this.cookieContainer = new CookieContainer();
    }

    public void GetAsync(Uri uri, Action<HttpWebResponse> onFinished)
    {
        var webRequest = HttpWebRequest.Create(uri) as HttpWebRequest;
        webRequest.Method = WebRequestMethods.Http.Get;
        webRequest.CookieContainer = this.cookieContainer;

        new Thread(() =>
        {
            HttpWebResponse httpResponse;
            try
            {
                httpResponse = webRequest.GetResponse() as HttpWebResponse;
            }
            catch (WebException ex)
            {
                if (onFinished != null)
                {
                    onFinished(ex.Response as HttpWebResponse);
                }
                return;
            }


            if (httpResponse.Cookies != null && httpResponse.Cookies.Count > 0)
            {
                this.cookieContainer.Add(httpResponse.Cookies);
            }

            if (onFinished != null)
            {
                onFinished(httpResponse);
            }

            httpResponse.GetResponseStream().Dispose();
        }).Start();
    }
}

System.Net.HttpWebRequest and System.Net.WebClient work on most platforms supported by Unity. System.Net.HttpWebRequestSystem.Net.WebClient在Unity支持的大多数平台上工作。 However when you want to build for the Unity Web Player or WebGL you will run into problems since Unity does not support most of the System.Net networking stuff since javascript does not have direct access to IP Sockets. 但是,当您要为Unity Web Player或WebGL进行构建时,会遇到问题,因为Unity不支持大多数System.Net网络内容,因为javascript无法直接访问IP套接字。 WebGL network restictions WebGL网络限制

As you already mentioned UnityWebRequest or the legacy WWW object from Unity is your best bet. 正如您已经提到的, UnityWebRequest或Unity的旧版WWW对象是您最好的选择。 With Unity 5.3 UnityWebRequest work on most platforms including WebGL and the Unity Web player. 使用Unity 5.3, UnityWebRequest可以在大多数平台上运行,包括WebGL和Unity Web Player。 But as you also already mentioned the complete UnityWebRequest is still experimental but is under constant development and will probably improve in every new update. 但是正如您已经提到的那样,完整的UnityWebRequest仍处于试验阶段,但仍在不断开发中,并且可能会在每个新更新中有所改进。

The only downside, using the WWW or UnityWebRequest object is ( as far as I understood the UnityWebRequest object) that they need to run in the Unity main thread so you will have to use Coroutines instead of pushing the request into a different thread. 使用WWWUnityWebRequest对象的唯一缺点是( 据我了解UnityWebRequest对象),它们需要在Unity主线程中运行,因此您将必须使用Coroutines而不是将请求推送到另一个线程中。 As long as you do not have millions of webrequest this should not lead into any performance issues of your app. 只要您没有数百万的Web请求,就不会导致应用程序出现任何性能问题。 And is probably less error prone. 而且可能不太容易出错。

The bigger issue is that WWW and UnityWebRequest do not support keep-alive on most platforms at the moment (WEBGL might be an exception, actually). 更大的问题是, WWWUnityWebRequest目前在大多数平台上不支持保持活动状态(实际上,WEBGL可能是一个例外)。 Expect any SSL encrypted requests to each have a huge amount of overhead (300+ milliseconds on a good machine). 期望任何SSL加密请求对每个请求都有大量开销(一台好的计算机上300毫秒以上)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM