WWW class and UnityWebRequest no longer working in Unity 2017.3.0
Hi guys,
I've been tasked with porting one of our apps from Unity 5.6.0 to Unity 2017.3.0. This app runs on the following platforms:
Windows Standalone
Windows Universal Store App
iOS
and Android
So far, the Windows Standalone version works fine. The problems start with the Windows Univeral Store App version: With this platform I don't seem to be able to make any kind of http or https request. Previously when using Unity 5.6.0 we used the www class to make these requests. Here is a code snippet of the original code:
if (Application.internetReachability != NetworkReachability.NotReachable)
{
// Construct header and append the encrypted XML string to the headers byte array.
byte[] aRequest = AppendHeader(strXMLRequest);
WWW cWWW = new WWW(m_strURL, aRequest);
while (!cWWW.isDone)
yield return null;
try
{
m_strXMLResponse = cWWW.text;
}
catch
{
Debug.LogError("Download error!");
m_strXMLResponse = "";
}
}
This code no longer works under Windows Universal platform. cWWW.text
is always empty. Now I realize that the WWW
class is deprecated, so I decided to rewrite the code so it uses the UnityWebRequest
class.
Here is another short code snippet:
UnityWebRequest uwr = UnityWebRequest.Post(m_strURL,"");
yield return uwr.SendWebRequest();
if(uwr.isNetworkError)
{
Debug.Log("Error While sending: " + uwr.error);
}
else
{
Debug.Log("Received: " + uwr.downloadHandler.text);
}
However with this code uwr.isNetworkError
is always true
and uwr.error
is always set to: "Generic/unknown http error." This is the case for any kind of http or https url. So at the moment ist seems I can not perform any kind of http or https request using unity classes in Unity version 2017.3.0. I downloaded Unity 5.6.0 again just to make sure it wasn't something to do with my computer or maybe some new settings that the sys admins made to the company firewall. - the code worked fine. Going back in Unity versions I could see that the code works up unitl Unity version 2017.2.1. So it would seem this is some kind of bug or regression within Unity 2017.3.0. I've tried all the patch versions of Unity 2017.3.0 and the 2018 Beta version and the problem persists in all these versions.
Could someone please confirm if this is really an unintended bug and if there is some kind of workaround? If it is a Unity bug I would say this is very critical. Any help or feedback would be greatly appreciated.
Cheers...
https. But I've also tried it with a http URL. Our server never receives the request.
In my case, my server does receive the request but yield return uwr.SendWebRequest(); never returns
I tried using wwwForm ins$$anonymous$$d of byte data, but the result is the same. The post itself and the headers send (via WWW), but the data is getting lost in the process.
Can you get any coroutines to work in that script other than sendwebrequest? such as: StartCoroutine(Test());
public IEnumerator Test() { yield return new WaitForSeconds(1f); Debug.Log("yielded fine."); }
Do you have a server and are able to make requests to it, and what do the logs look like on the server end? does it log a good response?
Yes, I have a server. But it's not receiving anything. However I've reverted back to Unity 2017.2.1 and everything works fine again. When Unity 2017.3.1 comes out I'll give it another go.
Answer by FiveFingers · Feb 02, 2018 at 10:14 PM
Yeah...they can't make it right...too many tasks. Switching back to 5.6
Answer by fernforce · Feb 03, 2018 at 10:18 AM
I couldn't make UnityWebRequest work either so I started using some .net stuff (I guess) to access web stuff...
WebClient client = new WebClient();
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
and for binary data:
WebClient client = new WebClient();
Stream data = client.OpenRead(repositoryForTerrains + terrainName + ".raw");
byte[] raw = new BinaryReader(data).ReadBytes(sizeOfTerrainFile);
What I posted here is not acceptable solution so I got unitywebrequest to work by setting the script containing my unitywebrequest code as component to another object. Furthermore, I discovered that coroutines stop and won't continue when you setActive(false) and setActive(true) once again on the object containing the script. Here is reference to someone who answered this particular issue that may solve someone's issue unitywebrequest: https://answers.unity.com/questions/34169/does-deactivating-a-gameobject-automatically-stop.html
Answer by yuyenkan999 · Feb 20, 2019 at 08:01 AM
disable chunkedTransfer, add accept and user-agent header on request, should be work.,disable chunkedTrasnfer, set accept and user-agent on header, should be work
Answer by Keyserjaya99 · Mar 25, 2019 at 06:17 AM
I've fixed it by set "www.chunkedTransfer = true;"
Thank you. But we switched to Unity 2018.x.x a long time ago. The old initial versions of Unity 2017 were just too buggy for our needs.