c# - unity www.progress 总是返回0

标签 c# web unity3d

统一 5.3.4p1 Mac OS X 10.11.5

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class TestWWW : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers["Api-key"] = "sorry-i-cant-post-this";
        headers["Content-Type"] = "application/json";
        string url =  "http://sorry-i-cant-post-this";
        var req = string.Format(
            "{{\"major\":{0},\"minor\":{1},\"patch\":{2},\"release\":{3},\"notice\":{4}}}",
            0, 0, 0, 40, 0
        );
        byte[] postData = Encoding.UTF8.GetBytes(req);

        var www = new WWW(url, postData, headers);
        StartCoroutine(F(www));
    }

    IEnumerator F(WWW www) {
        while(!www.isDone) {
            Debug.Log(www.progress);
            yield return null;
        }
        if(www.error == null)
            Debug.Log("Done");
        else 
            Debug.Log(www.error);

    }

    // Update is called once per frame
    void Update () {

    }
}

此代码始终打印 0,因为 F(WWW www) 函数的 while 循环无法完成。

我用Charles监控,发现Response Code是405 Method Not Allowed。

请求是

POST /api/client/v2 HTTP/1.1 User-Agent: UnityPlayer/5.3.4p1 (http://unity3d.com) Host: http://sorry-i-cant-post-this Accept: */* Accept-Encoding: identity Api-key: sorry-i-cant-post-this Content-Length: 55 Content-Type: application/json X-Unity-Version:
5.3.4p1

{"major":0,"minor":0,"patch":0,"release":40,"notice":0}

有什么问题吗?

或者这只是 Unity 的一个错误?

谢谢!!!

最后我发现是因为我打开了ShadowSocks。

最佳答案

当服务器未返回内容长度 header 时会发生这种情况。在您的服务器代码中,您应该添加 header Content-Length:,后跟要发送给客户端的数据的大小。

如果那不能解决问题,则使用 UnityWebRequest API。移植问题中的代码以使用 UnityWebRequest 而不是 WWW

// Use this for initialization
void Start()
{

    string url = "http://sorry-i-cant-post-this";
    var req = string.Format(
        "{{\"major\":{0},\"minor\":{1},\"patch\":{2},\"release\":{3},\"notice\":{4}}}",
        0, 0, 0, 40, 0
    );

    UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Post(url, req);
    www.SetRequestHeader("Api-key", "sorry-i-cant-post-this");
    www.SetRequestHeader("Content-Type", "application/json");

    StartCoroutine(F(www));
}

IEnumerator F(UnityEngine.Networking.UnityWebRequest www)
{
    www.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
    www.Send();

    while (!www.isDone)
    {
        Debug.Log(www.downloadProgress);
        yield return null;
    }

    if (www.isError)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("Done");
        Debug.Log("Downloaded: " + www.downloadHandler.text);

        // Or retrieve results as binary data
        byte[] results = www.downloadHandler.data;
    }

}

// Update is called once per frame
void Update()
{

}

关于c# - unity www.progress 总是返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37674562/

相关文章:

c# - Newtonsoft如何读取多个json的内容?

c# - 我如何在 C# 中显示来自 url 内容的小图像

unity3d - 3D模型有尺寸信息吗?

c# - 在 Unity 中使用 Resources.Load 时搜索所有子文件夹

c# - 将 DateTime 格式化为英文速记月份

c# - 客户端在短时间内发送太多消息时丢失消息

apache - 相对路径的 SSL?

http - Measurement Protocol HTTP 批量事件 POST 而不是每个事件的 POST?

c# - 在命令行中使用 MSTSC 传输文件

javascript - 如何通过音频切换向网页添加背景音乐?