GLe*_*aTi 1 c# rest unity-game-engine asp.net-web-api
我在asp.net web api2服务器上有这个代码:
[HttpPost]
[AllowAnonymous]
public IHttpActionResult Login(RequestLogin args)
{
//work
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用参数从" 高级REST客户端 " 发送发送请求:
http://localhost:54382/api/Home/Login
application/json
{"Name":"asd","DeviceId":"818cd41169410e6f20ef55b2917d0e15e5cf072f"}
Run Code Online (Sandbox Code Playgroud)
......它完美无缺!Login方法中的args 已满.
但如果我从Unity3d尝试这个:
UnityWebRequest www;
var url = http://localhost:54382/api/Home/Login;
string jsonObj = JsonUtility.ToJson(request.args);
Debug.Log(jsonObj); //{"Name":"asd","DeviceId":"818cd41169410e6f20ef55b2917d0e15e5cf072f"}
www = UnityWebRequest.Post(url, jsonObj);
www.SetRequestHeader("Content-Type", "application/json; charset=utf-8");
yield return www.Send();
Run Code Online (Sandbox Code Playgroud)
Login方法中的args包含空值.
[Serializable]
public class RequestLogin
{
public string Name;
public string DeviceId;
}
Run Code Online (Sandbox Code Playgroud)
我打破了好几天.请帮帮我
小智 8
将Unity升级到2017.3.0版后,我遇到了完全相同的问题.对我有用的修复是将UnityWebRequest.chunkedTransfer设置为false.我的post方法如下所示:
public static UnityWebRequest PostJson(string url, string json)
{
UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(json);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.chunkedTransfer = false;
return request;
}
Run Code Online (Sandbox Code Playgroud)