root / Assets / Plugins / UnityHTTP / README.md @ 9:37719e88568d
History | View | Annotate | Download (3.1 kB)
1 | # Attribution |
---|---|
2 | |
3 | Based on Simon Wittber's UnityWeb code (http://code.google.com/p/unityweb/). |
4 | |
5 | # About |
6 | |
7 | This is a TcpClient-based HTTP library for use in Unity. It should work in |
8 | both the standalone player and in the web player. |
9 | |
10 | It also has convenience methods for working with JSON. |
11 | # Examples |
12 | |
13 | IEnumerator example: |
14 | |
15 | ```C# |
16 | public IEnumerator SomeRoutine() { |
17 | HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" ); |
18 | someRequest.Send(); |
19 | |
20 | while( !someRequest.isDone ) |
21 | { |
22 | yield return null; |
23 | } |
24 | |
25 | // parse some JSON, for example: |
26 | JSONObject thing = new JSONObject( request.response.Text ); |
27 | } |
28 | ``` |
29 | |
30 | Closure-style (does not need to be in a coroutine): |
31 | |
32 | ```C# |
33 | HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" ); |
34 | someRequest.Send( ( request ) => { |
35 | // parse some JSON, for example: |
36 | JSONObject thing = new JSONObject( request.response.Text ); |
37 | }); |
38 | ``` |
39 | |
40 | Post request using form data: |
41 | |
42 | ```C# |
43 | WWWForm form = new WWWForm(); |
44 | form.AddField( "something", "yo" ); |
45 | form.AddField( "otherthing", "hey" ); |
46 | |
47 | HTTP.Request someRequest = new HTTP.Request( "post", "http://someurl.com/some/post/handler", form ); |
48 | someRequest.Send( ( request ) => { |
49 | // parse some JSON, for example: |
50 | bool result = false; |
51 | Hashtable thing = (Hashtable)JSON.JsonDecode( request.response.Text, ref result ); |
52 | if ( !result ) |
53 | { |
54 | Debug.LogWarning( "Could not parse JSON response!" ); |
55 | return; |
56 | } |
57 | }); |
58 | ``` |
59 | |
60 | Post request using JSON: |
61 | |
62 | ```C# |
63 | Hashtable data = new Hashtable(); |
64 | data.Add( "something", "hey!" ); |
65 | data.Add( "otherthing", "YO!!!!" ); |
66 | |
67 | // When you pass a Hashtable as the third argument, we assume you want it send as JSON-encoded |
68 | // data. We'll encode it to JSON for you and set the Content-Type header to application/json |
69 | HTTP.Request theRequest = new HTTP.Request( "post", "http://someurl.com/a/json/post/handler", data ); |
70 | theRequest.Send( ( request ) => { |
71 | |
72 | // we provide Object and Array convenience methods that attempt to parse the response as JSON |
73 | // if the response cannot be parsed, we will return null |
74 | // note that if you want to send json that isn't either an object ({...}) or an array ([...]) |
75 | // that you should use JSON.JsonDecode directly on the response.Text, Object and Array are |
76 | // only provided for convenience |
77 | Hashtable result = request.response.Object; |
78 | if ( result == null ) |
79 | { |
80 | Debug.LogWarning( "Could not parse JSON response!" ); |
81 | return; |
82 | } |
83 | |
84 | }); |
85 | ``` |
86 | |
87 | If you want to make a request while not in Play Mode (e. g. from a custom Editor menu command or wizard), you must use the Request synchronously, since Unity's main update loop is not running. The call will block until the response is available. |
88 | |
89 | ```C# |
90 | Hashtable data = new Hashtable(); |
91 | data.Add( "something", "hey!" ); |
92 | data.Add( "otherthing", "YO!!!!" ); |
93 | |
94 | HTTP.Request theRequest = new HTTP.Request("post", "http://someurl.com/a/json/post/handler", data ); |
95 | theRequest.synchronous = true; |
96 | theRequest.Send((request) => { |
97 | EditorUtility.DisplayDialog("Request was posted.", request.response.Text, "Ok"); |
98 | }); |
99 | ``` |
100 |