xLua icon indicating copy to clipboard operation
xLua copied to clipboard

UnityWebRequest请求byte[]数据,转string后,无法使用,拿不到正确的数据

Open 850176300 opened this issue 2 years ago • 0 comments

在lua侧调用UnityWebRequest请求第三方tts文字转音频的接口,获取到bytes数组,结果DownloadHanderBuffer里的data从byte[]转string,变成了空字符串。在C#侧用相同的逻辑请求可以获取正确的数据,看到byte数组以多个0开头,会不会是底层C++从char*转string的时候遇到'\0'就结束了,导致转出来的string不对,也无法还原成byte[],有没有什么方案可以规避这个问题呢 local params = { text=text, nd_lang="zh-CHS", ent="intp65", aue="raw", sfl="1", auf="audio/L16;rate=8000", vcn=realVoice, speed=speed, volume=volume, pitch=pitch, reg="2", } cs_coroutine.start(function() local data = json.encode(params); cast(CS.System.Text.Encoding.UTF8, typeof(CS.System.Text.Encoding)) local raw = CS.System.Text.Encoding.UTF8:GetBytes(data); local request = WebRequest(getTTSUrl(), WebRequest.kHttpVerbPOST); request.uploadHandler = CS.UnityEngine.Networking.UploadHandlerRaw(raw); request.downloadHandler = CS.UnityEngine.Networking.DownloadHandlerBuffer(); request:SetRequestHeader("Accept", "application/json") request:SetRequestHeader("Content-Type", "application/json") request:SetRequestHeader("sdp-app-id", AccountManager:GetInstance():GetSdpAppId()) local host = getHost(); request:SetRequestHeader("host", host); coroutine.yield(request:SendWebRequest()); if request.isNetworkError or request.error then Utils.Log("ParseText, request Api Failed "..request.error) else local bytes = request.downloadHandler.data; local bytesLen = request.downloadedBytes; --此处bytes=“” end end)

850176300 avatar Apr 27 '22 09:04 850176300