- Home /
Any way to speed up WWW requests?
I'm moving a multiplayer application of mine from XNA over to Unity so that I can make use of the web player. To make things work with the Web Player, it seems I have to use the WWW class instead of HttpWebRequest. My tests are showing that accessing the exact same page from a Unity application takes 10 times longer using WWW than it does using HttpWebRequest. Is that to be expected? Is there anything I can do to improve WWW's performance?
In my specific case, the WWW request takes about 450 ms (around half a second) to fully execute, while the HttpWebRequest is taking 47 ms - less than half of a tenth of a second!
Answer by Max Kaufmann · Oct 28, 2010 at 02:23 AM
WWW is also encoding the download into proper objects - are you accounting for the post-download initialization in your comparison?
Is there any way to avoid the encoding/processing?
It appears WWW is pretty inefficient.
Answer by mikeytrw · Sep 21, 2011 at 10:39 AM
I've had some success increasing WWW request performance by building the POST data myself rather than using the WWWForm object:
private string generatePostString(){
//MW: Generate a random salt for this request
System.Random random = new System.Random();
int randomNumber = random.Next(0, 9999999);
//MW: hash the salt and secret key
string salt = randomNumber.ToString();
string hashedKey = Md5Sum(mSecretKey + salt);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("hashedKey="+ hashedKey +"&salt="+ salt +"&deviceType=5&bindingVersion=0.1");
foreach (DictionaryEntry item in mParameters){
//MW: TODO We need to distinguish between data types and correctly handle each one.
sb.AppendLine("¶ms["+item.Key.ToString()+"]="+item.Value.ToString());
}
return sb.ToString();
}
private byte[] convertPostStringToData(string postString){
//MW: convert the string into a byte array
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetBytes(postString);
}
Also, I suspect the additional time for a WWW request is because it has to load the crossdomain.xml for every request which is pretty annoying.
Your answer
![](https://koobas.hobune.stream/wayback/20220613080605im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is HttpWebRequest supposed to work in Web Builds or not? 1 Answer
Is HttpWebRequest supported for desktop and editor? 2 Answers
How to network a large map with 4000+ movable objects using Photon Unity Networking? 1 Answer
How do I properly send binary data (byte[]) to a MySQL database? 4 Answers
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers