Skip to main content
 首页 » 编程设计

c#之无法在 Unity 中使用 UnityWebRequest 从 HTTP 响应获取数据

2024年02月04日29bjzhanghao

嘿 Unity 和 Microsoft 专业人士!我正在尝试在 Unity 中使用 Microsoft Bing Text to Speech API,此 API 需要将在请求 header 中传递的 AccessToken。为了获得这个 token ,我已经在 "Ocp-Apim-Subscription-Key" 中发送了我的Authentication Key header ,API 将返回我接下来可以使用的访问 token ,这是对在 Postman 中重新调整 token 的 API 的测试。

所以这是执行此操作的代码,但它不起作用。

using System.Collections; 
using System.Collections.Generic; 
using System.Xml.Linq; 
using UnityEngine; 
using UnityEngine.Networking; 
 
public class Test : MonoBehaviour 
{ 
    public static readonly string accessUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; 
    public string accessToken; 
 
    public void Start() 
    { 
        WWWForm wwwForm = new WWWForm(); 
        Dictionary<string, string> headers = wwwForm.headers; 
        headers["Ocp-Apim-Subscription-Key"] = "a66ec1e2123784hf39f22e2dc2e760d13x"; 
 
        UnityWebRequest www = UnityWebRequest.Post(accessUri, wwwForm); 
        StartCoroutine(RequestToken(www)); 
    } 
 
    public IEnumerator RequestToken(UnityWebRequest www) 
    { 
        yield return www; 
        if (www.error == null) 
        { 
            Debug.Log("downloadedBytes : " + www.downloadedBytes); 
            Debug.Log("certificateHandler : " + www.certificateHandler); 
            Debug.Log("chunkedTransfer : " + www.chunkedTransfer); 
            Debug.Log("downloadHandler : " + www.downloadHandler); 
            Debug.Log("downloadProgress : " + www.downloadProgress); 
            Debug.Log("isDone : " + www.isDone); 
            Debug.Log("isNetworkError : " + www.isNetworkError); 
            Debug.Log("method : " + www.method); 
            Debug.Log("redirectLimit : " + www.redirectLimit); 
            Debug.Log("responseCode : " + www.responseCode); 
            Debug.Log("uploadedBytes : " + www.uploadedBytes); 
            Debug.Log("useHttpContinue : " + www.useHttpContinue); 
        } 
        else 
        { 
            Debug.Log("Error" + www.error); 
        } 
        var p = www.downloadHandler.data; 
        Debug.Log("Access token: " + p); 
    } 
} 

这段代码的结果:

我已经尝试过WWW 类,但这没有用!和 System.Net.Http,但 Unity 不会接受此库:/

请问有什么办法吗?

请您参考如下方法:

我认为您需要一个 www.SendWebRequest() 语句。您的代码只是声明 yield return www;,而不是 yield return www.SendWebRequest()。

请参阅此代码示例(来自 UnityEngine.Networking.UnityWebRequest.Post 文档):

using UnityEngine; 
using UnityEngine.Networking; 
using System.Collections; 
 
public class MyBehavior : MonoBehaviour 
{ 
    void Start() 
    { 
        StartCoroutine(Upload()); 
    } 
 
    IEnumerator Upload() 
    { 
        WWWForm form = new WWWForm(); 
        form.AddField("myField", "myData"); 
 
        using (UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", form)) 
        { 
            yield return www.SendWebRequest(); 
 
            if (www.isNetworkError || www.isHttpError) 
            { 
                Debug.Log(www.error); 
            } 
            else 
            { 
                Debug.Log("Form upload complete!"); 
            } 
        } 
    } 
} 

(此外,关于 Access token: System.Byte[] 输出消息和您的代码,请注意应该使用 DownloadHandler.text 属性而不是 DownloadHandler.data 用于调试输出。目前,它只是打印属性的类型而不是其实际内容。)

编辑:请注意,我以这种方式调试问题,因为 www.isDone 为 false 而 www.downloadProgress 为 -1。这暗示 www 请求从未正确发送或完成。如果这是一个错误,我认为 www.isDone 可能在其他地方提供的错误情况下也是如此。