简体   繁体   中英

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.

Searching around with Google, I see the best I can do is manually set the headers of request and get the response header. I am ok with that, because I am originally ASP.NET MVC developer.

However, I then realize they use Dictionary for both the request and response. Now that's the problem. We know that the header can be duplicated, in my case is the 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; 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). However, I see no documentation on the .NET Framework compability in Unity. Can anyone tell me if it is supported on most platform? My main targets are Windows, Android and Web. If possible, even for WebClient would be nicer.

Here is my current solution, which work good in the Unity Editor, but I have yet to test them on other devices. 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. 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. WebGL network restictions

As you already mentioned UnityWebRequest or the legacy WWW object from Unity is your best bet. With Unity 5.3 UnityWebRequest work on most platforms including WebGL and the 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.

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. As long as you do not have millions of webrequest this should not lead into any performance issues of your app. 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). Expect any SSL encrypted requests to each have a huge amount of overhead (300+ milliseconds on a good machine).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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