1
|
using System.Collections;
|
2
|
using System.Collections.Generic;
|
3
|
using UnityEngine.Networking;
|
4
|
using UnityEngine.UI;
|
5
|
using UnityEngine;
|
6
|
|
7
|
|
8
|
public class Client : MonoBehaviour
|
9
|
{
|
10
|
public InputField postField;
|
11
|
public InputField getField;
|
12
|
public InputField messageField;
|
13
|
public Text responseField;
|
14
|
|
15
|
//public string postAddress = "";
|
16
|
//public string getAddress = "";
|
17
|
public string responseMsg = "Response will show up here...";
|
18
|
|
19
|
void Start()
|
20
|
{
|
21
|
responseField.text = responseMsg;
|
22
|
}
|
23
|
|
24
|
public void OnUploadButtonPressed()
|
25
|
{
|
26
|
StartCoroutine(Upload());
|
27
|
}
|
28
|
|
29
|
IEnumerator Upload()
|
30
|
{
|
31
|
Debug.Log("Uploading: " + messageField.text);
|
32
|
UnityWebRequest www = UnityWebRequest.Post(postField.text, messageField.text);
|
33
|
yield return www.SendWebRequest();
|
34
|
|
35
|
if (www.isNetworkError || www.isHttpError)
|
36
|
{
|
37
|
Debug.Log(www.error);
|
38
|
}
|
39
|
else
|
40
|
{
|
41
|
Debug.Log("Upload complete!");
|
42
|
}
|
43
|
}
|
44
|
public void OnDownloadButtonPressed()
|
45
|
{
|
46
|
StartCoroutine(GetText());
|
47
|
}
|
48
|
|
49
|
IEnumerator GetText()
|
50
|
{
|
51
|
Debug.Log("Downloading...");
|
52
|
UnityWebRequest www = UnityWebRequest.Get(getField.text);
|
53
|
yield return www.SendWebRequest();
|
54
|
|
55
|
if (www.isNetworkError || www.isHttpError)
|
56
|
{
|
57
|
Debug.Log(www.error);
|
58
|
}
|
59
|
else
|
60
|
{
|
61
|
// Show results as text
|
62
|
Debug.Log(www.downloadHandler.text);
|
63
|
responseField.text = www.downloadHandler.text;
|
64
|
|
65
|
// Or retrieve results as binary data
|
66
|
// byte[] results = www.downloadHandler.data;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
}
|