• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Antho · Jul 15, 2011 at 12:43 AM · wwwurlunsupported

Doesn't WWW like HTTP 302 (Redirect)?

Hello!

Below is a very simple REST class around the WWW object. It sends a HTTP request, calls the callback when necessary and protects it in case of bad request. The actual class is slightly more complicated ;)

 public delegate void RequestCallback (WWW request);
 public delegate void DataCallback (string text);
 public class REST : MonoBehaviour
 {
     public DataCallback MakeRequestCallback(APICallback callback)
     {
         return (request) => 
         {
             if( !string.IsNullOrEmpty( request.error ) ) 
             {
                 Debug.LogError( "Error during request: " + request.error );
             }
             else if( c!=null ) 
             {
                 callback(request.text);
             }
         };
     }
     public IEnumerator MakeRequest (string url, RequestCallback callback)
     {
         WWW request = new WWW(url);
         return WaitForRequest(request, callback);
     }
 }


When I want to fetch data, I can do something like this (from inside a MonoBehaviour):

 // REST rest is accessed on the Awake() of the MonoBehaviour
 // doSomething could be print or Debug.LogError
 string url = "http://mydomain.com/myscript.php";
 RequestCallback callback = rest.MakeRequestCallback( doSomething ); 
 StartCoroutine(rest.MakeRequest(url, callback));


Now all this works fine, unless the script on the server does something funky like a HTTP 302 redirection (which translates in PHP as header("Location: path"); )

Let's say for example that I have static ASCII data files and a PHP script to pick one randomly. The most natural way would be to redirect to that random file.

If I do that, the following message appears in my console:

"Error during request: unsupported url"

It could be the extension of the file that makes it an ugly URL. But I don't think so: I printed the "unsupported" URL in the console from the if-block of "Error during request" and it was exactly the path to my PHP script.

I solved my problem by dumping the contents of the chosen file into the return of the script, but I feel it's not as scalable as a redirection would have been. And not a good practice in general (some of the files can be lengthy).

I haven't tried to access the file directly from a WWW request yet, I will do and update this post, however I found no reference on "unsupported URLs" or redirect headers with WWW, so I thought it would be good to share. As I use iOS, I cannot use the other framework WebRequest/HTTPRequest.

Is it a bug? Is it the expected behavior?

Thanks, Antho

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jul 15, 2011 at 01:09 AM

The www class implements only a simple http request. It just wraps a normal tcp socket, nothing more. All it can do is send the request to the server and it waits for the response. Like the http status codes explains only status codes that start with 2xx are "success". A redirection is a case where the client / user "must take additional action to complete the request". The client have to do a new seperate request to get the actual file.

Unfortunately the WWW class doesn't provide the status code so you can't check it yourself. What you can do is check the (still undocumented) .responseHeaders for a "location" header field and preform another get request.

( I guess you use visual studio so you shouldn't have problems to use responseHeaders ;) )

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Antho · Jul 15, 2011 at 01:18 AM 0
Share

I use $$anonymous$$onoDevelop on OS X :) I'll look at responseHeaders, thanks a lot for the answer!

avatar image Bunny83 · Jul 15, 2011 at 02:42 AM 0
Share

Oh, my bad. I don't use $$anonymous$$onoDevelop since it's too buggy ;) But it should also have autocompletion and show it as a member of the WWW class. Just in case someone else don't have it: .responseHeaders is a Dictionary. To get one of the response header fields just use www.responseHeaders["fieldname"] after the request is done.

 Debug.Log("Location: "+ www.responseHeaders["Location"]);  
 Debug.Log("Cookie: "+ www.responseHeaders["Set-Cookie"]);  
avatar image kixorz · Sep 07, 2012 at 03:23 PM 0
Share

FYI www.responseHeaders is not available on iOS in Unity 3.x

avatar image
0

Answer by Simon Wittber · Feb 08, 2012 at 03:22 AM

Alternatively, scrub WWW altogether and use UniWeb. You can do anything HTTP related with it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

can iphone load jpeg from url 1 Answer

is it possible to get the url from a webpage? 1 Answer

Unity3D WWW takes URL from address bar 1 Answer

Max String Length in WWW url's? 1 Answer

WWW.EscapeURL Not Work Well? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges