I have written the following co-routine that send a JSON post request to webservice that also return a JSON object which can be 30-40MB large containing some data. This JSON object contains a base64 string which can be decoded into binary data of any type. After decoding, I want to saved this binary data as a file on the local machine. I want to make the whole process as asynchronous as possible and so far the download progress from WWW.progress is working fine but I encounter a strange block when accessing WWW.text:
IEnumerator WWWJSONPost(string url, string jsonString)
{
var encoding = new System.Text.ASCIIEncoding();
byte[] formData = encoding.GetBytes(jsonString);
//Create the headers for this POST
Hashtable theHeaders = new Hashtable();
print("Request Headers:");
theHeaders.Add("Content-Type", "application/json");
print("Content-Type,application/json");
theHeaders.Add("Content-Length", formData.Length);
print("Content-Type,"+formData.Length);
theHeaders.Add("Accept", "application/json");
print("Accept,application/json");
string userpwd = user + ":" + password;
string encodedUserpwd = Base64Codec.EncodeTo64(userpwd);
theHeaders.Add("Authorization", "Basic " + encodedUserpwd);
print("Authorization," + "Basic " + encodedUserpwd);
//++++++
//The blocking WWW object that initiate request upon construction
WWW theWWW = new WWW(url, formData , theHeaders);
while(!theWWW.isDone)
{
//Report progress
if(m_ProgressHandler != null)
{
m_ProgressHandler(theWWW.progress);//Works
}
yield return null;
}
yield return theWWW;
//When www.text is access, all following codes are blocked for a while (1-2 sec)
print("Output...");
//Do something with received data
print("WWW byte count: " + theWWW.bytes.Length);
if(theWWW.error != null)
{
print("Error: " + theWWW.error);
yield break;
}
print("Deserializing...");
byte[] WWWBytes = theWWW.bytes;//Very fast, does not block
string JSONString = theWWW.text;//blocks for awhile for a www with byte count 37205271, when this line is commented, no blocking occurs
yield return null;
}
Initially, I thought that creating another coroutine to decode the www.text would work but it did not. Now I’m pretty much stuck here. Is there anyway to prevent the block or should I just allow it to happen?