Unity:在Unity3D中使用HTTP PUT [英] Unity: Use HTTP PUT in Unity3D
问题描述
我是Unity的新手,在Unity中遇到RESTFul的一些问题。
我想使用HTTP PUT更新服务器上的一些数据,但正如我在搜索网页时收到的那样,Unity中的WWWW类不支持HTTP PUT。我还尝试了一些与HTTP PUT相关的HttpWebRequest示例,但始终收到错误代码400:错误请求。
I'm quite new to Unity and facing some problems about RESTFul in Unity. I want to update some data on the server by using HTTP PUT, but as what I received when search the web, the WWWW class in Unity doesn't support HTTP PUT. I also tried some HttpWebRequest example related to HTTP PUT but always received error code 400: Bad Request.
如何解决此问题?
我必须在更新时列出所有键值对,或者只需要列出我想要更改值的对吗?
How can I solve this problem? Do I have to list out all the key-value pairs when updating or just need to list the pairs I want to change the value ?
推荐答案
我使用HttpWebRequest通过以下代码使其工作
I made it worked by the following codes using HttpWebRequest
void updatePlayer(){
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://yourAPIUrl");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{" +
"'ID': '100'," +
"'ClubName': 'DEF'," +
"'Number': 102," +
"'Name': 'AnNT'," +
"'Position': 'GK'," +
"'DateOfBirth': '2010-06-15T00:00:00'," +
"'PlaceOfBirth': 'Hanoi'," +
"'Weight': 55," +
"'Height': 1.55," +
"'Description': 'des'," +
"'ImageLink': 'annt.png'," +
"'Status': false," +
"'Age': '12'" +
"}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
Debug.Log(responseText);
}
}
这篇关于Unity:在Unity3D中使用HTTP PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!