andyburke / UnityHTTP

A TcpClient-based HTTP library for Unity.
GNU General Public License v2.0
592 stars 146 forks source link

Cross-domain policy / webplayer #55

Closed GarethIW closed 9 years ago

GarethIW commented 9 years ago

Hope this isn't "stupid question" territory, but here goes.

I'm trying to use the https://www.leaderboards.io webapi from Unity using UnityHTTP. It's working fine in desktop builds. In webplayer builds, the following exception occurs:

SecurityException: Unable to connect, as no valid crossdomain policy was found

I'm making a PUT request asynchronously like so:

HTTP.Request theRequest = new HTTP.Request("put", "https://api.leaderboards.io/leaderboard/063f5e89611ff77b/score", data);
    theRequest.Send((request) =>
    {
                  Debug.LogError(request.exception);
...

A crossdomain.xml file exists at https://api.leaderboards.io/crossdomain.xml. I get the policy not found exception when running in the editor (webplayer platform) and also with the webplayer (hosted locally on IIS and also tried on Azure).

I tried doing a Fiddler debug, but the request doesn't show at all (in either the desktop build or webplayer - not sure if it should!)

Unity version 5.1.2f1, by the way.

Any ideas?

andyburke commented 9 years ago

Check this out, let me know if it helps: https://github.com/andyburke/UnityHTTP/issues/1

GarethIW commented 9 years ago

That did help, but it's not an easy issue to solve if you don't have full control over the server you're hosting the webplayer on.

I decided to take a different route - using webplayer browser calls to the host page and run the webapi calls using jquery. I still use UnityHTTP calls for the other platforms, and refactored my callbacks so they work with both UnityHTTP and javascript.

In case anyone is having the same fun as me, here's a gist for my workaround: https://gist.github.com/GarethIW/0ecdbba5083f39547f76