WWW/UnityWebRequest POST/GET请求不会从server/url返回最新数据

Kil*_*con 5 c# yield get unity-game-engine hololens

我正在使用Unity创建一个HoloLens应用程序,它必须从REST API获取数据并显示它.我目前正在使用WWW数据类型来获取将从Update()函数调用的协程中的数据和yield return语句.当我尝试运行代码时,我从API获取最新数据,但当有人将任何新数据推送到API时,它不会实时自动获取最新数据,我必须重新启动应用程序以查看最新数据.我的代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;

public class TextChange : MonoBehaviour {

    // Use this for initialization
    WWW get;
    public static string getreq;
    Text text;
    bool continueRequest = false;

    void Start()
    {
        StartCoroutine(WaitForRequest());
        text = GetComponent<Text>();
    }

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

    }

    private IEnumerator WaitForRequest()
    {
        if (continueRequest)
            yield break;

        continueRequest = true;

        float requestFrequencyInSec = 5f; //Update after every 5 seconds 
        WaitForSeconds waitTime = new WaitForSeconds(requestFrequencyInSec);

        while (continueRequest)
        {
            string url = "API Link goes Here";
            WWW get = new WWW(url);
            yield return get;
            getreq = get.text;
            //check for errors
            if (get.error == null)
            {
                string json = @getreq;
                List<MyJSC> data = JsonConvert.DeserializeObject<List<MyJSC>>(json);
                int l = data.Count;
                text.text = "Data: " + data[l - 1].content;
            }
            else
            {
                Debug.Log("Error!-> " + get.error);
            }

            yield return waitTime; //Wait for requestFrequencyInSec time
        }
    }

    void stopRequest()
    {
        continueRequest = false;
    }
}

public class MyJSC
{
    public string _id;
    public string author;
    public string content;
    public string _v;
    public string date;
}
Run Code Online (Sandbox Code Playgroud)

Pro*_*mer 7

发生这种情况是因为在服务器上启用了资源缓存.

我知道的三种可能的解决方案:

1.Disable资源缓存在服务器上.每个Web服务器的说明都不同.通常在.htaccess.

2.使用唯一的时间戳生成每个请求.时间应该是Unix格式.

此方法不适用于iOS.你很好,因为这是为了HoloLens.

例如,如果您的网址是http://url.com/file.rar,则追加?t=currentTime到最后.currentTimeUnix格式的实际时间.

完整示例网址: http://url.com/file.rar?t=1468475141

代码:

string getUTCTime()
{
    System.Int32 unixTimestamp = (System.Int32)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds;
    return unixTimestamp.ToString();
}

private IEnumerator WaitForRequest()
{
    string url = "API Link goes Here" + "?t=" + getUTCTime();
    WWW get = new WWW(url);
    yield return get;
    getreq = get.text;
    //check for errors
    if (get.error == null)
    {
        string json = @getreq;
        List<MyJSC> data = JsonConvert.DeserializeObject<List<MyJSC>>(json);
        int l = data.Count;
        text.text = "Data: " + data[l - 1].content;
    }
    else
    {
        Debug.Log("Error!-> " + get.error);
    }
}
Run Code Online (Sandbox Code Playgroud)

3.通过提供和修改请求中的和头来Disable缓存客户端.Cache-ControlPragma

设置Cache-Control 标题max-age=0, no-cache, no-store然后设置Pragma 标题no-cache.

我建议你这样做UnityWebRequest而不是WWW上课.首先,包括using UnityEngine.Networking;.

代码:

IEnumerator WaitForRequest(string url)
{

    UnityWebRequest www = UnityWebRequest.Get(url);
    www.SetRequestHeader("Cache-Control", "max-age=0, no-cache, no-store");
    www.SetRequestHeader("Pragma", "no-cache");

    yield return www.Send();
    if (www.isError)
    {
        Debug.Log(www.error);
    }
    else
    {
        Debug.Log("Received " + www.downloadHandler.text);
    }
}
Run Code Online (Sandbox Code Playgroud)