15266

Generic/Unknown HTTP Error with response code 0 using UnityWebRequest

I was trying to use some server's API functions, and suddenly came to this error. I will explain it after the code sample:

public IEnumerator Post(Dictionary<string, string> data) { var request = UnityWebRequest.Post(Url, data); request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); yield return request.Send(); if (request.isError) UpdateNetworkLog("Network error has occured: " + request.error); else // Some code after success }

As you can see, mostly I took it from the Unity manual considering POST requests here. But, there is one problem - the request never finished correctly, it always has error (which is shown using UpdateNetworkLog function): "Generic/unknown HTTP error", while request's response code is 0. Also, I've tried to use legacy method from this manual and WWW class getting the same result.

I think, problem is in how Unity gets the response: I have checked packets via Wireshark, both my POST request and server's response are perfectly fine, no errors or mistakes.

Another important issue is that I'm using WebGL, and everything's all right in Editor, program gets the proper response and works correctly; error appears only after I build and run the game in the browser (tested in Chrome and Safari). Also, tried launch it using not the Unity WebGL Player, but XAMPP: the same.

Thanks for all the future responses, as Google does not know anything about it.

Answer1:

Potential Solution 1

From the Unity documentation on using UnityWebRequest with WebGL:

The WWW and UnityWebRequest classes are supported in WebGL. They are implemented using the XMLHttpRequest class in JavaScript, using the browser to handle WWW requests. This imposes some security restrictions on accessing cross-domain resources. Basically any WWW request to a server which is different from the server hosting the WebGL content needs to be authorized by the server you are trying to access. To access cross-domain WWW resources in WebGL, the server you are trying to access needs to authorize this using CORS.

My best guess is that this server that you are trying to access does not have CORS correctly set up or configured. When your code is running in a browser, the browser itself is handling the request and consequently subjugating your code to that browser's security restrictions. Your program and the server aren't on the same domain, and the browser doesn't like that.

If this is your problem, then without more information it's difficult for me to suggest a solution. From your post, it sounds like you might be relatively new to coding (or at least to HTTP-related coding), and the best solutions I can find to your problem involve some relatively complex methods, such as using JavaScript instead of UnityWebRequest.

Potential Solution 2

Of course, the actual problem might even lie with your surrounding code instead, or with the server you're trying to access. What is your code for the function that calls Post()? There could be some issue with order-of-events that's showing up only when the code is run in a browser (where JavaScript is handling the POST request).

On a side note, if the server you're trying to access has documentation for their API, your problem might be addressed somewhere in it.

Answer2:

The first thing to point out is that UnityWebRequest actually returns the response in

<strong>request.downloadHandler.text</strong>

While only returning the general error information in

<strong>request.error</strong>

<hr>

Now if your case is like mine and you upgraded Unity only to have all your networking code break, the error you'll likely see is related to <strong>Content-Length</strong>

This is because for both the WWW and UnityWebRequest class now have <strong>chunkedTransfer on by default</strong>. This appears to screw up how content length is sent over and causes the error.

While chunkedTransfer can currently be disabled for UnityWebRequest, it cannot be disabled for the WWW class at the time of this writing. For me this meant rewriting all my old network code to use UnityWebRequest instead and set

<strong>myUnityWebRequest.chunkedTransfer = false;</strong>

Note: UnityWebRequest does not currently support POSTs with no data. You still need to use the WWW class for this.

Recommend

  • Broadcast on different subnets
  • How to obtain amount of transferred data through Wi-Fi from other applications?
  • Why does Internet Explorer encode the URL differently when Control-clicking a link
  • Move saved TortoiseSVN authentication to another machine?
  • python: using raw socket with OSX
  • QT background process for keyboard input sniffing
  • can't receive UDP packages when wire-shark is off
  • Is it possible to have a forward proxy with ssl encryption between the proxy and the user?
  • C++11 variable size POD struct
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • How to make R's read_csv2() recognise the text characters properly
  • Query to find the duplicates between the name and number in table
  • How to revert to previous XCode version?
  • Eloquent update method change created_at timestamp
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Deselecting radio buttons while keeping the View Model in synch
  • Nant, Vault & Windows Integrated Authentication
  • Optimizing database types to compact database (SQLite)
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Cross-Platform Protobuf Serialization
  • Where to put my custom functions in Wordpress?
  • Trying to switch camera back to front but getting exception
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Do I've to free mysql result after storing it?
  • How can I estimate amount of memory left with calling System.gc()?
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • Free memory of cv::Mat loaded using FileStorage API
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • unknown Exception android
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app