小编Tai*_* Wu的帖子

Unity:使用JSON使用WWW类的POST请求

我正在尝试向Unity中的restful Web API发出POST请求.

标题是 Content-Type: application/json

原始数据输入的一个示例是,其中data是键,json字符串是值:

{  
   "data":{  
      "username":"name",
      "email":"email@gmail.com",
      "age_range":21,
      "gender":"male",
      "location":"california"
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的脚本:

private static readonly string POSTAddUserURL = "http://db.url.com/api/addUser";
public WWW POST()
{
    WWW www;
    Hashtable postHeader = new Hashtable();
    postHeader.Add("Content-Type", "application/json");
    WWWForm form = new WWWForm();
    form.AddField("data", jsonStr);
    www = new WWW(POSTAddUserURL, form);
    StartCoroutine(WaitForRequest(www));
    return www;
}

IEnumerator WaitForRequest(WWW data)
{
    yield return data; // Wait until the download is done
    if (data.error != null)
    {
        MainUI.ShowDebug("There was an error sending request: " + …
Run Code Online (Sandbox Code Playgroud)

c# rest post request unity-game-engine

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

标签 统计

c# ×1

post ×1

request ×1

rest ×1

unity-game-engine ×1