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);

}

}


'중국인턴(Intern In China) > 업무(Tasks)' 카테고리의 다른 글

Change Color of Z Sticks(Theater Mode)  (0) 2017.01.11
Unity Http Put Json Type  (0) 2017.01.11
Parsing data from Json  (0) 2017.01.10
Loaded Json Data from Server@@@  (0) 2017.01.06
Unity read Json data  (0) 2017.01.06

+ Recent posts