- Home /
Unity not passing space character to WWW url.
I have a problem when I try to access any URL like this by script:
mysite = WWW("http://localhost/mylist.php?team="+"No team");
The "No team" string has a space character, and I get no output from the URL in Unity. But, if I go to the browser and type the same URL: "http://localhost/mylist.php?team=No team", the URL works all right, and I get the results.
It looks like Unity isn't passing the space character to the URL, how can I solve this problem?
( this URL lists all users where team == "No team" )
Answer by DaveA · Mar 09, 2012 at 08:51 PM
Someone else on Unity Answers mentioned that WWW.EscapeURL would not correctly escape characters: http://answers.unity3d.com/questions/598817/c-unity-function-to-escape-url-querystring-paramet.html
In case it would be of use to someone, there's also a .NET way of doing it, mentioned in the answer above.
Answer by Bunny83 · Mar 09, 2012 at 06:34 PM
The answer is quite simple: space characters are not allowed in urls...
You've heared about escaping special characters?
This is the escaped URL of "http://localhost/$$anonymous$$mlist.php?$$anonymous$$m=No Team" :
"http%3a%2f%2flocalhost%2f$$anonymous$$mlist.php%3f$$anonymous$$m%3dNo%2520Team"
You can see also that the escape function switches the space character to "%2520". Which doesn't work. I changed the encoding parameters, but it's always the same output.
Altrough, if I switch the space character to %20, it works fine.
I could change the space character to "%20", I tried the string.Replace function, but had no sucess yet. I'll keep trying and tell if I solve the problem.
Ok, I got it:
*var site = "http://localhost/$$anonymous$$mlist.php?$$anonymous$$m=No Team";
var newSite = site.Replace(' ','%20');*
The output is "http://localhost/$$anonymous$$mlist.php?$$anonymous$$m=No%20Team.
You shouldn't use EscapeURL on the whole url. You have to escape your data fields.
Something like that:
var $$anonymous$$m = "No $$anonymous$$m";
mysite = WWW("http://localhost/mylist.php?$$anonymous$$m="+WWW.EscapeURL($$anonymous$$m));
The escape function is there to be able to include those special characters in your data that would mess up the URL format. You shouldn't escape the necessary control characters that actually make a string a URL.
On my computer a space char is escaped to "+" but "%20" is also fine. Your url looks like you escaped it two times...
"%25" == "%"
"%2520" == "%20" == " "
Your answer