「Unity/Csharp/Request」の版間の差分
提供: 初心者エンジニアの簡易メモ
行1: | 行1: | ||
− | + | public class ComScript : MonoBehaviour { | |
− | + | public void Exec () { | |
− | + | string url = "ttp://hogehoge.com/api?name1=" | |
− | + | string param = "value1"; | |
− | + | url += WWW.EscapeURL (param); | |
− | + | StartCoroutine(Get (url)); | |
− | + | } | |
− | + | public IEnumerator Get (string url) { | |
− | + | var request = new UnityWebRequest(); | |
− | + | request.downloadHandler = new DownloadHandlerBuffer(); | |
− | + | request.url = url; | |
− | + | request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8"); | |
− | + | request.method = UnityWebRequest.kHttpVerbGET; | |
− | + | yield return request.Send(); | |
− | + | if (request.isError) { | |
− | + | Debug.Log(request.error); | |
− | + | } else { | |
− | + | if (request.responseCode == 200) { | |
− | + | Debug.Log(request.downloadHandler.text); | |
− | + | ||
} | } | ||
+ | } | ||
} | } | ||
− | + | } |
2017年10月1日 (日) 05:19時点における版
public class ComScript : MonoBehaviour { public void Exec () { string url = "ttp://hogehoge.com/api?name1=" string param = "value1"; url += WWW.EscapeURL (param); StartCoroutine(Get (url)); } public IEnumerator Get (string url) { var request = new UnityWebRequest(); request.downloadHandler = new DownloadHandlerBuffer(); request.url = url; request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8"); request.method = UnityWebRequest.kHttpVerbGET; yield return request.Send(); if (request.isError) { Debug.Log(request.error); } else { if (request.responseCode == 200) { Debug.Log(request.downloadHandler.text); } } } }