mingchaoyan
mingchaoyan

Reputation: 8606

Unity www.progress always return 0

Unity 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 () {

    }
}

This code always print 0, because while loop of F(WWW www) function cant finish.

I use Charles to monitor, find the Response Code is 405 Method Not Allowed.

and the request is

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}

Is there any thing wrong?

Or it is just a Unity bug?

Thanks!!!

Finally, I find it's because I open ShadowSocks.

Upvotes: 2

Views: 3536

Answers (1)

Programmer
Programmer

Reputation: 125275

This happens when the server is not returning the content length header. In your server code, you should add the header Content-Length: followed by the size of the data you are sending to the client.

If that doesn't solve the problem, then use the UnityWebRequest API. Ported the code in your question to use UnityWebRequest instead of 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()
{

}

Upvotes: 1

Related Questions