- Home /
What is a good cross-platform alternative to WWW?
I've been integrating the Firebase API in Unity for a few days now. The WWW class is a bit tricky to wrangle, but I got it working as intended with Firebase's basic RESTful API. To do this, I force a WWW object to operate on POST as such:
string someUrl = "<some firebase URI>";
byte[] postData = new byte[]{0}; //some non-null value needed to force WWW to use POST
Dictionary<string,string> headers = new Dictionary<string,string>();
//as per Firebase's API docs, specify HTTP method in this header
headers.Add("x-http-method-override","GET");
WWW webRequest = new WWW(someUrl, postData, headers);
However, in attempting to use this same strategy for querying Firebase's Streaming API, the WWW object I construct isn't handling "307 Temporary Redirect" response codes as needed. An error is silently caught inside webRequest and webRequest's error property is set to contain it. Although webRequest's status header indicates a "307 Temporary Redirect" response code, there is no "Location" header to redirect to. This was checked by dumping the keys and values in the webRequest.responseHeaders property.
The error is from its underlying cURL calls, saying "necessary data rewind wasn't possible". For reference, the cURL line with the error (assuming Unity3D uses the latest cURL on github): https://github.com/bagder/curl/blob/b0143a2a33f0e577b1c2c9a407ac081418b5ea6b/lib/transfer.c#L299.
Since no Location header is given for the redirect request, I cannot attempt to perform a redirect manually. There appears to be no way to force the WWW class to expose all headers it was sent, or to ensure it doesn't attempt an automatic redirect.
So, knowing that is an issue preventing use of the WWW class from using Firebase's Streaming API...
Are there any cross-platform alternatives for making HTTP requests, ideally built in to Unity for a better guarantee of ongoing support?
I see there's System.Net, but Googling suggests its cross-platform support is sketchy, and that it's mainly intended for Windows PCs.
Any suggestions / recommendations would be very much welcome! :) I think I've squeezed all I can out of the WWW class...
Have you tried to parse the response headers of the WWW response?
request.responseHeaders["STATUS"]
That should give you the information of a redirect, and then you could apply your logic to that specific response.
Here's the definition for the property:
http://docs.unity3d.com/ScriptReference/WWW-responseHeaders.html
Regards
Hey, thanks for trying to help :)
I must have been unclear in my original post - I have verified that the response header isn't available by dumping all keys and values from responseHeaders and inspecting their values both by eye and in code. I am doing this irrespective of the contents / null state of the error property.
Also, when dumping all responseHeaders, the response header "STATUS" does indicate a 307 Temporary Redirect. But no location is defined in any other header, though there should be a redirect URL available in the "LOCATION" header.
Do you need the redirect url from the response? or is that something that you want to manually specify?. If your scenario is the second one, here's an idea just to check if this applies.
You execute a http request using WWW
As part of your post request, you send a RedirectUrl parameter, with the url for the manual redirection
The Request responds with an Status of 307 Temporary Redirect
Your code "catches" this logic flow, and executes another WWW request to your RedirectUrl original parameter.
It's just an idea :$
If you want to perform a redirect with the "Location" header from the WWW response, I think the location is not available on this result (just for the 301, 302 status codes). I did a bit of research and with that status code and that result asumes that the server do not need to redirect, so that's the reason that no Location url is provided. I did a bit more research (I'm very curious about web requests / responses :D) and, with other languages and there's a post that suggests to respost exactly the same (params and method) when receiving a 307 status code.
http://programmers.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect
Give it a try
Here's another very well explained (and very loooong post) about the redirect status http://sebastians-pamphlets.com/the-anatomy-of-http-redirects-301-302-307/
Your answer
Follow this Question
Related Questions
Capturing and Tracing All HTTP Requests in Unity project 0 Answers
HttpWebRequest and HTTPS on iOS 0 Answers
About Implemented host name resolution with IPv6 in Unity 4.6.9 release-notes. 0 Answers
Using HttpWebRequest asynchronously with HTTPS 0 Answers
WebRequest.BeginGetRequestStream fails on iOS using IPv6 DNS64/NAT64 1 Answer