http://blog.naver.com/PostView.nhn?blogId=sonmg&logNo=220510568356
public enum EType {
GET,
POST,
PUT,
DELETE
};
[SerializeField] public EType _type = EType.GET;
private void OnButtonClicked()
{
Debug.Log ("OnButtonClicked... " + _type);
if (_type == EType.GET)
{
// GET
string url = "http://127.0.0.1:3000/method_get_test/users";
WWW www = new WWW (url);
StartCoroutine(WaitForRequest(www));
}
else if (_type == EType.POST)
{
// POST
string url = "http://127.0.0.1:3000/method_post_test/user";
WWWForm form = new WWWForm();
form.AddField("id", "8");
form.AddField("name", "brian8");
WWW www = new WWW (url, form);
StartCoroutine(WaitForRequest(www));
}
else if (_type == EType.PUT)
{
// PUT
string url = "http://127.0.0.1:3000/method_put_test/user/id/8/ddddd";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "PUT";
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
Debug.Log(responseText);
}
}
else if (_type == EType.DELETE)
{
// DELETE
string url = "http://127.0.0.1:3000/method_del_test/user/id/8";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "DELETE";
HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
Debug.Log(responseText);
}
}
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
if (www.error == null)
{
// request completed!
Debug.Log (www.text);
}
else
{
// something wrong!
Debug.Log ("WWW error: " + www.error);
}
}