Machineboy空

Http통신 본문

Computer

Http통신

안녕도라 2023. 11. 2. 12:54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(UnityWebRequestGet());
    }

    //01. GET: API에 저장되어 있는 정보를 받아오는 것
    IEnumerator UnityWebRequestGet()
    {
        //API 사이트의 주소 - request url
        //요청을 보낼 주소

        //1) 요청변수가 없는 경우
        //string apiKey = "";
        //string url = "https://api.neople.co.kr/df/servers?apikey=dZ5WTN1OAt5tpzMHnZr2mI6wLJfSp4OM";
        //string url = "https://api.neople.co.kr/df/servers?apikey="+apiKey;

        //string url = "https://api.neople.co.kr/df/servers?apikey=dZ5WTN1OAt5tpzMHnZr2mI6wLJfSp4OM";

        //2) 요청변수가 있는 경우
        //변수 이름 정확히 똑같이 적어줘야함.

        string serverId = "cain";

        string url = $"https://api.neople.co.kr/df/servers/{serverId}/characters-fame?minFame=<minFame>&maxFame=<maxFame>&jobId=<jobId>&jobGrowId=<jobGrowId>&isAllJobGrow=<isAllJobGrow>&isBuff=<isBuff>&limit=<limit>&apikey=dZ5WTN1OAt5tpzMHnZr2mI6wLJfSp4OM";


        //01. get방식으로 해당 주소에 요청을 보낸다.
        UnityWebRequest www = UnityWebRequest.Get(url);
        //02. 응답을 기다린다. 응답이 돌아올 때까지
        yield return www.SendWebRequest();

        //03. 응답이 성공적으로 왔을 때, 응답이 실패했을 때
        if (www.error == null)
        {
            //응답이 온 내용을 다운로드해서 text로 한 번 보자
            Debug.Log(www.downloadHandler.text);
        }
        else
        {
            Debug.Log("ERROR");
        }
    }

    //02. SET: API에 정보를 저장하는 것


}