Tai*_* Wu 1 c# rest post request unity-game-engine
我正在尝试向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: " + data.error);
}
else
{
MainUI.ShowDebug("WWW Request: " + data.text);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用WWW带有表单和标题的类发送请求?或者,一般来说,我如何发送此类邮件请求?
如果你想添加原始的json数据,最好不要传递它 WWWForm
public WWW POST()
{
WWW www;
Hashtable postHeader = new Hashtable();
postHeader.Add("Content-Type", "application/json");
// convert json string to byte
var formData = System.Text.Encoding.UTF8.GetBytes(jsonStr);
www = new WWW(POSTAddUserURL, formData, postHeader);
StartCoroutine(WaitForRequest(www));
return www;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18052 次 |
| 最近记录: |