6
6
7
7
namespace HttpMono
8
8
{
9
+ public static class UnityWebRequestExtensions
10
+ {
11
+ public static void AddHeaders ( this UnityWebRequest webRequest , Dictionary < string , string > headers )
12
+ {
13
+ if ( headers != null )
14
+ {
15
+ foreach ( var h in headers )
16
+ {
17
+ webRequest . SetRequestHeader ( h . Key , h . Value ) ;
18
+ }
19
+ }
20
+ }
21
+ }
22
+
9
23
public class HttpMono : MonoBehaviour
10
24
{
11
25
@@ -14,19 +28,13 @@ public void Get(string uri, Dictionary<string, string> headers = null, Action<Ht
14
28
StartCoroutine ( HttpGet ( uri , headers , resultCallback ) ) ;
15
29
}
16
30
17
- public IEnumerator HttpGet ( string uri , Dictionary < string , string > headers = null , Action < HttpRequestResult > resultCallback = null )
31
+ public IEnumerator HttpGet ( string url , Dictionary < string , string > headers = null , Action < HttpRequestResult > resultCallback = null )
18
32
{
19
- Debug . Log ( "GET: " + uri ) ;
33
+ Debug . Log ( "GET: " + url ) ;
20
34
21
- using ( UnityWebRequest webRequest = UnityWebRequest . Get ( uri ) )
35
+ using ( UnityWebRequest webRequest = UnityWebRequest . Get ( url ) )
22
36
{
23
- if ( headers != null )
24
- {
25
- foreach ( var h in headers )
26
- {
27
- webRequest . SetRequestHeader ( h . Key , h . Value ) ;
28
- }
29
- }
37
+ webRequest . AddHeaders ( headers ) ;
30
38
31
39
// Request and wait for the desired page.
32
40
yield return webRequest . SendWebRequest ( ) ;
@@ -38,11 +46,47 @@ public IEnumerator HttpGet(string uri, Dictionary<string, string> headers = null
38
46
}
39
47
40
48
resultCallback ? . Invoke ( result ) ;
49
+ }
50
+ }
41
51
52
+ public void Post ( string url , string postData , Dictionary < string , string > headers = null , Action < HttpRequestResult > resultCallback = null )
53
+ {
54
+ StartCoroutine ( HttpPost ( url , postData , headers , resultCallback ) ) ;
55
+ }
42
56
57
+ public IEnumerator HttpPost ( string url , string postData , Dictionary < string , string > headers = null , Action < HttpRequestResult > resultCallback = null )
58
+ {
59
+ Debug . Log ( "POST: " + url ) ;
43
60
61
+ Debug . Log ( "BODY: " + postData ) ;
62
+
63
+ using ( UnityWebRequest webRequest = new UnityWebRequest ( url , "POST" ) )
64
+ {
65
+ webRequest . AddHeaders ( headers ) ;
66
+ webRequest . SetRequestHeader ( "Content-Type" , "application/json" ) ;
67
+ webRequest . downloadHandler = new DownloadHandlerBuffer ( ) ;
68
+
69
+ UploadHandler jsonUploadHandler = new UploadHandlerRaw ( System . Text . Encoding . UTF8 . GetBytes ( postData ) ) ;
70
+ jsonUploadHandler . contentType = "application/json" ;
71
+ webRequest . uploadHandler = jsonUploadHandler ;
72
+
73
+ // Request and wait for the desired page.
74
+ yield return webRequest . SendWebRequest ( ) ;
75
+ var result = new HttpRequestResult ( webRequest ) ;
76
+
77
+ if ( result . Ok == false )
78
+ {
79
+ Debug . LogError ( result . Error . Message ) ;
80
+ }
81
+ if ( result . HasText )
82
+ {
83
+ Debug . Log ( result . Text ) ;
84
+ }
85
+
86
+ resultCallback ? . Invoke ( result ) ;
44
87
}
45
88
}
46
89
90
+
47
91
}
48
92
}
0 commit comments