无法在 Unity 中使用 UnityWebRequest 从 HTTP 响应获取数据
Cannot get data from HTTP Response using UnityWebRequest in Unity
嘿 Unity 和 Microsoft 专业人士!我正在尝试在 Unity 中使用 Microsoft Bing Text to Speech API,这个 API 需要将在请求 header 中传递的 AccessToken。为了获得这个令牌,我已经在 "Ocp-Apim-Subscription-Key" header,API 将 return 返回我接下来可以使用的访问令牌,这是对 API 的测试在 Postman 中返回令牌。
所以这是执行此操作的代码,但它不起作用。
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 class,但是没有用!和 System.Net.Http,但 Unity 不会接受此库:/
请问有什么办法吗?
尝试在您的请求中添加这些
request.Accept = @"application/json;text/xml";
request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000";
并检查它是否正常工作。
我认为您需要 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
可能是正确的,其他地方提供的错误。
嘿 Unity 和 Microsoft 专业人士!我正在尝试在 Unity 中使用 Microsoft Bing Text to Speech API,这个 API 需要将在请求 header 中传递的 AccessToken。为了获得这个令牌,我已经在 "Ocp-Apim-Subscription-Key" header,API 将 return 返回我接下来可以使用的访问令牌,这是对 API 的测试在 Postman 中返回令牌。
所以这是执行此操作的代码,但它不起作用。
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 class,但是没有用!和 System.Net.Http,但 Unity 不会接受此库:/
请问有什么办法吗?
尝试在您的请求中添加这些
request.Accept = @"application/json;text/xml";
request.ContentType = @"audio/wav; codec=audio/pcm; samplerate=16000";
并检查它是否正常工作。
我认为您需要 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
可能是正确的,其他地方提供的错误。