- Home /
How can I send and receive data to and from a URL, i.e. server side scripts, web services, etc?
How can I send data (eg, a string or integer value) to a server-side script or a web service through C# or Javascript in unity Web player mode.
Can you please give me some details on how to call web services. Me being new to Unity Web application, I dont know even how to call a URL and a parameter with it. So could anyone give a proper way to call Web services.
Answer by duck · Feb 03, 2010 at 05:24 PM
The normal way of doing this would be to use Get or Post requests via the WWW class or the WWWForm class. If you're making a Get request with parameters added to the end of the url, you'd use code similar to this:
'GET' request in Javascript:
var url = "http://example.com/script.php?var1=value2&var2=value2";
var www : WWW = new WWW (url);
// wait for request to complete
yield www;
// and check for errors
if (www.error == null)
{
// request completed!
} else {
// something wrong!
Debug.Log("WWW Error: "+ www.error);
}
'GET' request In C#
In C# this is a little more complex because of the way that you have to write out coroutines in full. In this example, the initiation of the request is done in "Start". We then hand the WWW object to a coroutine called "WaitForRequest" which waits for the www request to complete.
using UnityEngine;
public class GetURL : MonoBehaviour {
void Start () {
string url = "http://example.com/script.php?var1=value2&var2=value2";
WWW www = new WWW(url);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
And to make a Post request with the same parameters and values, it would be:
'POST' request in Javascript:
var url = "http://example.com/script.php";
var form = new WWWForm();
form.AddField( "var1", "value1" );
form.AddField( "var2", "value2");
var www = new WWW( url, form );
// wait for request to complete
yield www;
// and check for errors
if (www.error == null)
{
// request completed!
} else {
// something wrong!
Debug.Log("WWW Error: "+ www.error);
}
'POST' request in C#
Again, we use exactly the same Coroutine and WWWCompleted function as in the 'get' example. The only difference is that we build the WWWForm object, and use that to create the WWW request before handing it to the coroutine.
using UnityEngine;
public class PostURL : MonoBehaviour {
void Start () {
string url = "http://example.com/script.php";
WWWForm form = new WWWForm();
form.AddField("var1", "value1");
form.AddField("var2", "value2");
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
Does the WWW GET/POST approach... pass data back and forth from database to Unity Player without refreshing the page the player is running on? A refresh would be undesired as it would break the user's experience and the game would also restart. Or is this approach only valid for a shot at the database on startup and quitting of the Unity App? . the http://URL... would point to a pageless/codebehind file? never done one of those, not sure how I would do that if it is the way to implement it.
No, it does not cause a page refresh. The requests go via the browser, but occur "behind the scenes".
i try this code. it work on unity simulator 3.0 and 3.3(trail version). But for unity 3.3 after i build it it didn't work on my android device but 3.0 after i build it work on my android device. For your information im using nexus S and nexus one. is it the bug from Unity?
Does anyone know if this works for iOS/Android builds? I am trying to consume a web service in my Unity3d scene and I am targeting mobile platforms. I'd rather not invest in the liscense yet if there is no way to do this!
@tyinus: Sure, it works on all platforms. Only webbuilds have a crossdomain restriction which can be removed by a crossdomain policy.
Answer by marinl · Apr 09, 2013 at 08:31 PM
I write a tutorial about http using in unity http://unity-tutorials.blogspot.com. Listing 12 - 15. I am using subroutines for http calls to not stop UI rendering.
Answer by gaminggal39 · Nov 12, 2018 at 07:51 AM
Watch this video to solve :https://www.youtube.com/watch?v=FChVgb6EXGk&t=133s
Your answer
Follow this Question
Related Questions
How to access StartCoroutine in a static way 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How can i receive data from URL like cookies? 0 Answers
Nested coroutines 6 Answers
How do I make a virtual world in Unity? 3 Answers