Note: UNet is deprecated, and will be removed from Unity in the future. A new system is under development. For more information and next steps see this blog post and the FAQ. |
There are two primary functions for sending data to a server formatted as a HTML form. If you are migrating over from the WWW system, see Using WWWForm, below.
To provide greater control over how you specify your form data, the UnityWebRequest
system contains a user-implementable IMultipartFormSection
interface. For standard applications, Unity also provides default implementations for data and file sections: MultipartFormDataSection
and MultipartFormFileSection
.
An overload of UnityWebRequest.POST
accepts, as a second parameter, a List argument, whose members must all be IMultipartFormSections
. The function signature is:
WebRequest.Post(string url, List<IMultipartFormSection> formSections);
UnityWebRequest
and sets the target URL to the first string parameter. It also sets the Content-Type header of the UnityWebRequest
appropriately for the form data specified in the list of IMultipartFormSection
objects.DownloadHandlerBuffer
to the UnityWebRequest
. This is for convenience - you can use this to check your server’s replies.WWWForm POST
function, this HLAPIA system for building multiplayer capabilities for Unity games. It is built on top of the lower level transport real-time communication layer, and handles many of the common tasks that are required for multiplayer games. More infoIMultipartFormSection
in turn and formats them into a standard multipart form as specified in RFC 2616.UploadHandlerRaw
object, which is then attached to the UnityWebRequest
. As a result, changes to the IMultipartFormSection
objects performed after the UnityWebRequest.POST
call are not reflected in the data sent to the server.using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
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("http://www.my-server.com/myform", formData);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}
}
To help migrate from the WWW system, the UnityWebRequest system permits you to use the old WWWForm object to provide form data.
In this case, the function signature is:
WebRequest.Post(string url, WWWForm formData);
UnityWebRequest
and sets the target URL to the first string argument’s value. It also reads any custom headers generated by the WWWForm
argument (such as Content-Type) and copies them into the UnityWebRequest
.DownloadHandlerBuffer
to the UnityWebRequest
. This is for convenience - you can use this to check your server’s replies.WWWForm object
and buffers it in an UploadHandlerRaw
object, which is attached to the UnityWebRequest
. Therefore, changes to the WWWForm
object after calling UnityWebRequest.POST
do not alter the contents of the UnityWebRequest
.using UnityEngine;
using System.Collections;
public class MyBehavior : public MonoBehaviour {
void Start() {
StartCoroutine(Upload());
}
IEnumerator Upload() {
WWWForm form = new WWWForm();
form.AddField("myField", "myData");
UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", form);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}
}
Did you find this page useful? Please give it a rating: