Original address: http://blog.csdn.net/iamyococo/article/details/44209243
the Unity script's approach to network requests is as follows:
Public WWW (string URL, byte[] postdata, dictionary<string, string> headers)
Public WWW (string URL, byte[] postdata, Hashtable headers)-----> Deprecated
Public WWW (string URL, byte[] postdata)
Public WWW (string url, wwwform form)
Public WWW (string url)
There are a lot of ways that you can see from the documentation.
But while I was reading the process of discovering a way has been abandoned, so deliberately write down notes to enhance memory.
Refer to "Unity 3d/2d mobile phone game development" a book self-study.
But unfortunately in the Unity5 when there is a way to be discarded, my ideas will be based on the book to record.
1. Create a script, choose C #, named Webmanager.
2. Adding script WebManager.cs to an object triggers the event of the script.
3. Start writing code.
3.1 Write an interface first.
Using Unityengine;
Using System.Collections;
public class Webmanager:monobehaviour {
//global variable, used to receive informational hints, initialized to "nothing".
string m_info = "Nothing";
void Ongui () {
GUI. BeginGroup (New Rect (Screen.width * 0.5f-100, Screen.height * 0.5f-100, M), "");
Create a label that sets the position, size, and the global variable that will receive the message as the text of the label.
GUI. Label (New Rect (), m_info);
Create a button, set the position, size, and the caption on the button is "Get Data".
if (GUI. button (new Rect (X, M,), "Get Data") {
//Here is the action, event triggered by the write-click button.
}
Create a button, set the position, size, and the caption on the button is "Post Data."
if (GUI. button (new Rect (Ten,,), "Post Data") {
//Here is the action, event triggered by the write-click button.
}
Gui. Endgroup ();
}
Use this for initialization
void Start () {
}
//Update are called once per frame
void Update () {
}
}
Let's start with a simple method.
Get requests are the simplest, so the generally simple method is the Get method.
3.2Get method
Write down this igetdata () function first. Note that the return type of the function is an iterator IEnumerator, which can be invoked together.
IEnumerator Igetdata () {
//access HTTP address using get method
www www = new www. ("http://yococoxc.vicp.cc:9999/test/ userprint.php?username=yococo&password=123456789 ");
Wait for the response of the server
yield return www;
If there
is an error if (Www.error!= null) {
//Get the server error message
m_info = www.error;
yield return null;
}
Gets the response text
M_info = Www.text for the
server
Then you need to set the above function on the Get button to trigger it.
if (GUI. button (new Rect (), "Get Data")) {
startcoroutine (Igetdata ());
Description
The function of the Startcoroutine () method is to start the collaboration program, execute the specified method, and of course the return type of the method to be executed must be IEnumerator.
The PHP code used for the program will be listed at the end.
The effect is:
3.3Post method
Get method is as simple as that, followed by the Post method, some similar, but more troublesome, of course, the Post method has many advantages, the default you know.
IEnumerator Ipostdata () {
dictionary<string,string> headers = new dictionary<string, string> ();
headers ["content-type"] = "application/x-www-form-urlencoded";
The post text content that will be sent
string data = "username=yococo&password=123456789";
Convert text to byte array
byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes (data);
Submit post data to HTTP server
www www = new www ("http://yococoxc.vicp.cc:9999/test/userprint.php", BS, headers);
Wait for the response of the server
yield return www;
If there
is an error if (Www.error!= null) {
//Get the server error message
m_info = www.error;
yield return null;
}
Gets the response text of the server
m_info = www.text;
Special tips:
Using the Dictionary class, please introduce a using System.Collections.Generic; Otherwise there will be an error. Then the button corresponds to the method that is executed.
if (GUI. button (new Rect (), "Post Data")) {
startcoroutine (Ipostdata ());
Results:
3.4 Disposal methods.
Public WWW (string URL, byte[] postdata, Hashtable headers)
Is discarded, this programming is not uncommon, there will be a relatively alternative to the disposal of the document is the key.
3.5 additional post notation to implement another method.
IEnumerator Ipostdata () {
//Post text content to be sent
string data = "username=yococo&password=123456789";
Convert text to byte array
byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes (data);
Submit post data to HTTP server
www www = new www ("http://yococoxc.vicp.cc:9999/test/userprint.php", BS);
Wait for the response of the server
yield return www;
If there
is an error if (Www.error!= null) {
//Get the server error message
m_info = www.error;
yield return null;
}
Gets the response text
M_info = Www.text for the
server
Add header information less here
IEnumerator Ipostdata () {
wwwform form = new Wwwform ();
Add a field (key, value)
form. AddField ("username", "Yococo");
Form. AddField ("Password", "123456789");
Submit post data to HTTP server, submit form
www www = new www ("http://yococoxc.vicp.cc:9999/test/userprint.php", form);
Wait for the response of the server
yield return www;
If there
is an error if (Www.error!= null) {
//Get the server error message
m_info = www.error;
yield return null;
}
Gets the response text
M_info = Www.text for the
server
The next step is the PHP code:
<?php
if (isset ($_get[' username ')) && isset ($_get[' password ')) {
echo "get-> username be". $_get[' username ']. "And password is". $_get[' password '];
else if (isset ($_post[' username ')) && isset ($_post[' password ')) {
echo "POST-> username is". $_post[' username ']. "And password is". $_post[' password '];
else {
echo ' error ';
}
? >