使用UnityWebRequest的响应代码为0的通用/未知HTTP错误 [英] Generic/Unknown HTTP Error with response code 0 using UnityWebRequest

查看:5214
本文介绍了使用UnityWebRequest的响应代码为0的通用/未知HTTP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用某些服务器的API函数,突然遇到了这个错误。我将在代码示例之后解释它:

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你可以看到,主要是我从Unity手册中考虑POST请求这里 。但是,有一个问题 - 请求从未正确完成,它总是有错误(使用UpdateNetworkLog函数显示):通用/未知HTTP错误,而请求的响应代码为0.此外,我尝试使用遗留从本手册和WWW类得到相同结果的方法。

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.

我认为问题在于Unity如何获得响应:我已经通过Wireshark检查了数据包,这两个我的POST请求和服务器的响应非常好,没有错误或错误。

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.

另一个重要的问题是我正在使用WebGL,并且编辑器中的一切都可以,程序得到正确的响应并正常工作;只有在浏览器中构建并运行游戏(在Chrome和Safari中测试)后才会出现错误。此外,尝试使用Unity WebGL播放器启动它,但XAMPP:相同。

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.

感谢所有未来的回复,因为Google对此一无所知。

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

推荐答案

潜在解决方案1 ​​



来自 Unity文档关于在WebGL中使用 UnityWebRequest

Potential Solution 1

From the Unity documentation on using UnityWebRequest with WebGL:


WebGL支持WWW和UnityWebRequest类。它们是使用JavaScript中的XMLHttpRequest类实现的,使用浏览器来处理WWW请求。这对访问跨域资源施加了一些安全限制。基本上,任何与托管WebGL内容的服务器不同的服务器WWW请求都需要由您尝试访问的服务器授权。要访问WebGL中的跨域WWW资源,您尝试访问的服务器需要使用CORS对其进行授权。

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.

我的最佳猜测是您尝试访问的此服务器没有正确设置或配置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.

如果这是您的问题,那么没有更多信息就很难我建议一个解决方案。从您的帖子来看,听起来您可能对编码(或至少与HTTP相关的编码)相对较新,而我能找到的最佳解决方案涉及一些相对复杂的方法,例如使用JavaScript 代替 UnityWebRequest

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.

当然,实际问题可能在于您的周围代码,或者与您尝试访问的服务器。调用 Post()的函数的代码是什么?只有当代码在浏览器中运行时(JavaScript正在处理POST请求),才会出现一些事件顺序问题。

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).

请注意,如果您尝试访问的服务器包含其API的文档,则可能会在其中的某处解决您的问题。

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.

这篇关于使用UnityWebRequest的响应代码为0的通用/未知HTTP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆