๐ŸŽฎ Unity Study/Unity

Unity HTTP ํ†ต์‹ (UnityWebRequest, Get/Post, IMultipartFormSection)

ibelieveinme 2023. 8. 7. 17:18
728x90

* HTTP: ์›น์ƒ์—์„œ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์— request/response ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํ”„๋กœํ† ์ฝœ.

 

HTTP ๋ฉ”์†Œ๋“œ ์ค‘ ๋งŽ์ด ์“ฐ๋Š” 2๊ฐ€์ง€. GET, POST.

 

1) GET: ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ๋ฉ”์†Œ๋“œ.

             ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ Body ์— ๋‹ด์ง€ ์•Š๊ณ  ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์„ ํ†ตํ•ด ์ „์†กํ•œ๋‹ค.

             URL์˜ ๋์— ?์™€ ํ•จ๊ป˜ ์ด๋ฆ„๊ณผ ๊ฐ’์œผ๋กœ ์Œ์„ ์ด๋ฃจ๋Š” ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

             ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ด๋ฉด &๋กœ ์—ฐ๊ฒฐํ•ด์„œ ์ „์†กํ•œ๋‹ค.

 

ex) ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์„ ํฌํ•จํ•œ URL ์ƒ˜ํ”Œ

www.example-url.com/resources?name1=value1&name2=value2

 

2) POST: ๋ฆฌ์†Œ์Šค๋ฅผ ์ƒ์„ฑ/๋ณ€๊ฒฝํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ ๋ฉ”์†Œ๋“œ.

                ์ „์†กํ•ด์•ผํ•  ๋ฐ์ดํ„ฐ๋ฅผ HTTP ๋ฉ”์„ธ์ง€์˜ Body์— ๋‹ด์•„์„œ ์ „์†กํ•œ๋‹ค.

                Body ์˜ ๊ธธ์ด์— ์ œํ•œ์—†์ด ๋Œ€์šฉ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค.

                Request Header ์˜ Content-Type ์— ์š”์ฒญ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค.

 

โ˜ž ์ฃผ์†Œ์— ์ •๋ณด๊ฐ€ ์žˆ์œผ๋ฉด GET ๋ฐฉ์‹์ด๊ณ , ์—†์œผ๋ฉด POST ๋ฐฉ์‹์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค.


Unity์—์„œ HTTP ํ†ต์‹ ์„ ํ•  ๋•Œ๋Š” UnityWebRequest ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

UnityWebRequest ์—์„œ Get ๋ฉ”์†Œ๋“œ์™€ Post ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ œ๊ณตํ•œ๋‹ค.

์ด ๋•Œ, Json ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋กœ ํ†ต์‹ ํ•œ๋‹ค๋ฉด JsonConvert.SerializeObject(๊ฐ์ฒด) or JsonConvert.DeserializeObject<๊ฐ์ฒดํ˜•> ๊ณผ์ •์œผ๋กœ ๋ณ€ํ™˜์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

 

1) UnityWebReqeust.Get

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
 
public class MyBehaviour : MonoBehaviour {
    void Start() {
        StartCoroutine(GetText());
    }
 
    IEnumerator GetText() {
        UnityWebRequest www = UnityWebRequest.Get("https://www.my-server.com");
        yield return www.SendWebRequest();
 
        if (www.result != UnityWebRequest.Result.Success) {
            Debug.Log(www.error);
        }
        else {
            // Show results as text
            Debug.Log(www.downloadHandler.text);
 
            // Or retrieve results as binary data
            byte[] results = www.downloadHandler.data;
        }
    }
}

UnityWebRequest.Get ์œผ๋กœ url์— ์ •๋ณด๋ฅผ ์š”์ฒญํ•œ ๋ชจ์Šต์ด๋‹ค.

๋ฐ›์•„์˜จ ๊ฒฐ๊ณผ๊ฐ’์„ ์ฝ”๋“œ ๋ฒ„ํผ์— ์ €์žฅํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด downloadHandler ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

2) UnityWebRequest.Post

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;

public class MyBehavior : MonoBehaviour {
    void Start() {
        StartCoroutine(Upload());
    }

    IEnumerator Upload() {
        List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
        formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
        formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));

        UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", formData);
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success) {
            Debug.Log(www.error);
        }
        else {
            Debug.Log("Form upload complete!");
        }
    }
}

UnityWebRequest.Post ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ์ดํ„ฐ๋Š” List<IMultipartFormSection> ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ Post ๋ฉ”์†Œ๋“œ ์ธ์ž๋กœ ๋ณด๋‚ธ๋‹ค.

์ด ๋•Œ, ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ๋Š” MultipartFormFileSection, ํŒŒ์ผ์ด ์•„๋‹Œ ์ผ๋ฐ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ๋Š” MultipartFormDataSection ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.


* ์ฐธ๊ณ 

 

UnityWebRequest - Unity ๋งค๋‰ด์–ผ

UnityWebRequest๋Š” HTTP ์š”์ฒญ์„ ๊ตฌ์„ฑํ•˜๊ณ  HTTP ๋ฆฌ์Šคํฐ์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ชจ๋“ˆ์‹ ์‹œ์Šคํ…œ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. UnityWebRequest ์‹œ์Šคํ…œ์˜ ์ฃผ์š” ๋ชฉํ‘œ๋Š” Unity ๊ฒŒ์ž„์ด ์ตœ์‹  ์›น ๋ธŒ๋ผ์šฐ์ € ๋ฐฑ ์—”๋“œ์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋„

docs.unity3d.com

 

 

HTTP ์„œ๋ฒ„์—์„œ ํ…์ŠคํŠธ ๋˜๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ(GET) - Unity ๋งค๋‰ด์–ผ

ํ‘œ์ค€ HTTP๋‚˜ HTTPS ์›น ์„œ๋ฒ„์—์„œ ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋‚˜ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด UnityWebRequest.GET ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋‹จ์ผ ๋ฌธ์ž์—ด์„ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ

docs.unity3d.com

 

HTTP ์„œ๋ฒ„๋กœ ์–‘์‹ ๋ณด๋‚ด๊ธฐ(POST) - Unity ๋งค๋‰ด์–ผ

HTML ์–‘์‹์œผ๋กœ ํฌ๋งท๋œ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๊ธฐ๋ณธ ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. WWW ์‹œ์Šคํ…œ์—์„œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์˜ WWWForm ์‚ฌ์šฉ์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

docs.unity3d.com

 

 

Unity - Scripting API: IMultipartFormSection

In order to provide a finer level of control for those wishing to generate multipart form data, but without forcing most users to refer to RFC 2388, Unity provides this simple interface which the UnityWebRequest API can use to serialize complex data into p

docs.unity3d.com

728x90