如何在Unity中使用WWW进行RESTful调用?

时间:2016-11-26 00:29:15

标签: c# unity3d web

我有一个Unity游戏,我正在努力调用RESTful API(这意味着我需要进行GET,POST,PUT和DELETE调用)。我尝试使用WWW类拨打这些电话,如下所示,但它无效:

public class GameRegistry : MonoBehaviour {
    public string SERVER = "my.ip.address.here/connections";

    private static string METHOD_HEADER = "X-HTTP-Method-Override";
    private static string CONTENT_HEADER = "content-type";
    private static string CONTENT_TYPE = "application/json";

    public void Start()
    {
        Debug.Log("Starting...");
        RESTHelper(SERVER, "", "DELETE");
        Debug.Log("...Finishing.");
    }

    private IEnumerator<WWW> RESTHelper(string url, string payload, string method)
    {
        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers.Add(METHOD_HEADER, method);
        headers.Add(CONTENT_HEADER, CONTENT_TYPE);

        Debug.Log("Request sent...");
        WWW request = new WWW(url, Encoding.UTF8.GetBytes(payload), headers);
        yield return request;
        Debug.Log("...reponse received.");
        Debug.Log(request.text);
    }

当我将它附加到GameObject并在我的游戏中运行时,我得到以下输出:

Starting...
...Finishing.

辅助方法中似乎没有执行任何操作。我做错了什么?

FWIW,我已经使用ARC测试了API,它运行良好。

0 个答案:

没有答案