Communication with server
Get is getting data from web server. and
new WWW("https://urlexample.com"); with a url but without a second parameter is doing a Get.
Post (Upload A File)
Upload a file to server is also a post. You can easily upload a file through WWW, like the below:
Upload A Zip File To Server
In this example, it use the coroutine to prepare and upload the file, if you want to know more about Unity coroutines, please visit Coroutines.
Sending a request to the server
There are many ways to communicate with servers using Unity as the client (some methodologies are better than others depending on your purpose). First, one must determine the need of the server to be able to effectively send operations to and from the server. For this example, we will send a few pieces of data to our server to be validated.
Most likely, the programmer will have setup some sort of handler on their server to receive events and respond back to the client accordingly - however that is out of the scope of this example.
First thing one must do is toss in their using statements which allow us to use the WebClient and NameValueCollection classes.
For this example the SendDataToServer function takes in 3 (optional) string parameters:
- Url of the server we're communicating with
- First piece of data
- Second piece of data we're sending to the server
The username and password is the optional data I am sending to the server. For this example we're using it to be then further validated from a database or any other external storage.
Now that we've setup our structure, we will instantiate a new WebClient to be used to actually send our data. Now we need to load our data into our NameValueCollection and upload the data to the server.
The UploadValues function takes in 3 necessary parameters as well:
- IP address of server
- HTTP method
- Data you're sending (the username and password in our case)
This function returns a byte array of the response from the server. We need to encode the returned byte array it into a proper string to actual be able to manipulate and dissect the response.
One could do something like this:
Now you might still be confused so I guess I will give a brief explanation of how to handle a response server sided.
For this example I will be using PHP to handle the response from the client. I'd recommend using PHP as your back-end scripting language because it's super versatile, easy to use and most of all fast. There definitely are other ways to handle a response on a server but in my opinion PHP is by far the simplest and easiest implementation into Unity.
So this is the most important part - the 'echo'. When our client uploads the data to server, the client saves the response (or resource) into that byte array. Once the client has the response, you know the data has been validated and you can move on in the client once that event has happened. You also need to think about what type of data you're sending (to an extent), and how to minimize the amount you're actually sending.
So this is only one way of sending/receiving data from Unity - there are some other ways that may be more effective for you depending on your project.
Simple Post (Post Fields)
Every instance of WWW with a second parameter is a post.
Here is an example to post user id and password to server.