标签: unitywebrequest

UnityWebRequest POST 不发送正文

正如标题所说,代码如下。我尝试设置 chunkedTransfer=false、Content-Type application/json、WWWForm、手动构建 JSON 对象和 HttpClient。对于 Content-Type application/json,API 甚至没有被命中。对于其余的,身体是一个空物体。通过 StackOverflow、YouTube、Unity 文档和所有其他资源,我不知道问题出在哪里。

截至今天早上,我正在使用 Newtonsoft.Json 来序列化 JSON 正文。我认为现在最大的问题是当我设置webRequest.SetRequestHeader("Content-Type", "application/json");API路由时甚至没有收到请求。

async Task<string> makeRequest()
        {
            string url = API_BASE + "/users"; 
            
            Dictionary<string, string> body = new Dictionary<string, string>();
            body.Add("username", username);
            body.Add("password", password);
            body.Add("email", email);

            using (UnityWebRequest webRequest = UnityWebRequest.Post(url, JsonConvert.SerializeObject(body)))
            {
                await webRequest.SendWebRequest();

                string result = Encoding.UTF8.GetString(webRequest.downloadHandler.data);

                if (webRequest.result != UnityWebRequest.Result.Success)
                {
                    JSONNode error = JSON.Parse(result);

                    registerAndLoginError.GetComponent<Text>().text = error["error"]["message"];
                    registerAndLoginError.SetActive(true);

                    return "";
                }
            }

            BasicLogin();
            return "";
        }
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine unitywebrequest

4
推荐指数
1
解决办法
7343
查看次数

如何使用 UnityWebRequest 从服务器下载 Asset Bundle 获取下载进度?

我还是新手,使用 UnityWebRequest 从服务器容器下载和加载资产包。问题是下载进度的值始终为 0。如何获取下载进度的值?

代码下面是我尝试下载并获取下载进度的内容。

//Method to download the assetbundle
IEnumerator DownloadAsset()
{
    string url = here the URL for asset bundle;
    using (var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
    {
        uwr.downloadHandler = new DownloadHandlerAssetBundle(url, 36, 0);
        UnityWebRequestAsyncOperation operation = uwr.SendWebRequest();
        yield return StartCoroutine(DownloadProgress(operation));

        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        {
            print("Get asset from bundle...");
        }


        //Load scene
        uwr.Dispose();
        print("ready to Load scene from asset...");
        StartCoroutine(LoadSceneProgress("Example"));
        bundle.Unload(false);
    }
}

//Method for download progress
IEnumerator DownloadProgress(UnityWebRequestAsyncOperation operation)
{
    while (!operation.isDone)
    {
        progressBar.color = Color.red; …
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine unitywebrequest

1
推荐指数
1
解决办法
8089
查看次数

如何使用 UnityWebRequest post api 调用发布我的数据

这是我的 API 请求

public IEnumerator Login(string bodyJsonString)
{
    Debug.Log(bodyJsonString);

    UnityWebRequest req = UnityWebRequest.Post("localhost:3000/login", bodyJsonString);
    req.SetRequestHeader("content-type", "application/json");
    yield return req.SendWebRequest();
    if (req.isNetworkError || req.isHttpError)
    {
        Debug.Log(req.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
    }

}
Run Code Online (Sandbox Code Playgroud)

它返回错误状态代码 500,并在服务器上返回错误 Unexpected token % in JSON atposition 0","severity

这是我的协程调用

public void submitLogin()
{

    _username = userInputField.GetComponent<InputField>().text;
    _password = passwordInputField.GetComponent<InputField>().text;

    Debug.Log("username" + _username);
    Debug.Log("password" + _password);

    string body = "{'username':'" + _username + "','password','" + _password + "'}";

    //API Call
    authChexi = new Auth();
    StartCoroutine(authChexi.Login(body));
}
Run Code Online (Sandbox Code Playgroud)

如果您对如何处理我的表单有任何想法,请告诉我。谢谢

c# json unity-game-engine unity-webgl unitywebrequest

1
推荐指数
1
解决办法
1万
查看次数