Could someone help me in my code it appears that WWW is obsolete and that I have to use UnityWebReques

huangapple go评论89阅读模式
英文:

Could someone help me in my code it appears that WWW is obsolete and that I have to use UnityWebReques

问题

public IEnumerator CO_Createuser(string user, string email, string pass, string repass, Action response)
{
WWWForm form = new WWWForm();
form.AddField("user", user);
form.AddField("email", email);
form.AddField("pass", pass);
form.AddField("repass", repass);
WWW w = new WWW("http://localhost:80/Game/createuser.php", form);
yield return w;
Debug.Log(w.text);
response(JsonUtility.FromJson(w.text));
}

I tried this but it didn't work either

UnityWebRequestweb = UnityWebRequest.Get("http://localhost:8080/webServicePlataformaDigital/Login.php" + "?alias=" + u + "&&password=" + p);

英文:
public IEnumerator CO_Createuser(string user, string email, string pass, string repass, Action<Response> response)
{
    WWWForm form = new WWWForm();
    form.AddField("user", user);
    form.AddField("email", email);
    form.AddField("pass", pass);
    form.AddField("repass", repass);
    WWW w = new WWW("http://localhost:80/Game/createuser.php", form);
    yield return w;
    Debug.Log(w.text);
    response(JsonUtility.FromJson<Response>(w.text));
}

I tried this but it didn't work either

UnityWebRequestweb = UnityWebRequest.Get("http://localhost:8080/webServicePlataformaDigital/Login.php"+"?alias="+u+"&&password="+p);

答案1

得分: 1

首先,是的,WWW 已经过时,不应再使用。

然后,从文档中可以看到,如果像您所做的那样在使用 WWW 时使用第二个参数 -

> WWW w = new WWW("http://localhost:80/Game/createuser.php", form);

隐式地使用了 POST 请求。

> WWW 类将默认使用 GET,如果您提供了 postData 参数,则会使用 POST

然而,除了这两者之间使用了完全不同的参数/字段之外,您一直在尝试使用带有 URL 嵌入字段的 GET 请求。

> UnityWebRequestweb = UnityWebRequest.Get("http://localhost:8080/webServicePlataformaDigital/Login.php"+"?alias="+u+"&&password="+p);

甚至更糟糕的是 - 在 URL 中包含密码以供所有人阅读 Could someone help me in my code it appears that WWW is obsolete and that I have to use UnityWebReques


您更希望使用 POST 请求,通过 UnityWebRequest.Post 来执行,例如:

var form = new WWWForm();
form.AddField("user", user);
form.AddField("email", email);
form.AddField("pass", pass);
form.AddField("repass", repass);

using(var request = new UnityWebRequest("http://localhost:80/Game/createuser.php", form))
{
    yield return request.SendWebRequest();

    if (request != UnityWebRequest.Result.Success)
    {
        Debug.LogError(request.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
        response(JsonUtility.FromJson<Response>(request.downloadHandler.text));
    }
}
英文:

First of all, yes, WWW is obsolete and should not be used anymore.

Then from the documentation you can see that WWW if used with a second parameter - like you do

> WWW w = new WWW("http://localhost:80/Game/createuser.php", form);

implicitly uses a POST request

> The WWW class will use GET by default and POST if you supply a postData parameter.

However - besides the fact that between these two you are using completely different parameters/fields - you have been trying using a GET request with URL embedded fields.

> UnityWebRequestweb = UnityWebRequest.Get("http://localhost:8080/webServicePlataformaDigital/Login.php"+"?alias="+u+"&&password="+p);

and even worse - including a password in the URL for everyone to read Could someone help me in my code it appears that WWW is obsolete and that I have to use UnityWebReques


What you rather want to do is using a POST request via UnityWebRequest.Post doing e.g.

var form = new WWWForm();
form.AddField("user", user);
form.AddField("email", email);
form.AddField("pass", pass);
form.AddField("repass", repass);

using(var request = new UnityWebRequest("http://localhost:80/Game/createuser.php", form))
{
    yield return request.SendWebRequest();

    if (request != UnityWebRequest.Result.Success)
    {
        Debug.LogError(request.error);
    }
    else
    {
        Debug.Log("Form upload complete!");
        response(JsonUtility.FromJson<Response>(request.downloadHandler.text));
    }
}

huangapple
  • 本文由 发表于 2023年6月29日 02:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定