/***************************************** Implementation *****************************************/
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
public class Sin_WS : MonoBehaviour
{
// Use this for initialization
private string charset = “UTF-8”;
private string url = “http://localhost/SINWS/test.php”;
private string tempuri = “http://localhost/SINWS/test.php/”;
private string prefixns = “ts”;
public string cookie=“”;
// ping
public Boolean ispingRunning = false;
private WWW webserviceping;
public string pingError=“”;
public List pingResult;
// End ping
// ping2
public Boolean isping2Running = false;
private WWW webserviceping2;
public string ping2Error=“”;
public TmpClase ping2Result;
// End ping2
private void Start()
{
return;
}
private void pingAsync(string ping_param)
{
string namefunction = “ping”;
Single soapVersion=1.1f;
string ss2 = SoapHeader(namefunction,soapVersion);
ss2 += “<” + prefixns + “:ping_param>” + Obj2XML(ping_param,true) + “</” + prefixns + “:ping_param>”;
ss2 += SoapFooter(namefunction,soapVersion);
ispingRunning = true;
StartCoroutine(pingAsync2(ss2));
}
private IEnumerator pingAsync2(string ss2) {
string namefunction = “ping”;
Single soapVersion=1.1f;
byte[ ] bb = System.Text.Encoding.UTF8.GetBytes(ss2);
var headers = header(namefunction,soapVersion);
if (cookie!=“”) {
headers.Add(“Set-Cookie”,cookie);
}
webserviceping = new WWW(url, bb, headers);
while( !webserviceping.isDone ) {
yield return new WaitForSeconds(0.5f);
}
ispingRunning = false;
string other = cleanSOAPAnswer(webserviceping.text, “ping”,ref pingError);
pingResult=new List();
pingResult=(List)XML2Obj(other,“ArrayOfString”,pingResult.GetType());
webserviceping.responseHeaders.TryGetValue(“SET-COOKIE”,out cookie);
pingAsyncDone();
}
public void pingAsyncDone() {
// we do something…
// List dnx=pingResult;
}
private void ping2Async(string ping_param)
{
string namefunction = “ping2”;
Single soapVersion=1.1f;
string ss2 = SoapHeader(namefunction,soapVersion);
ss2 += “<” + prefixns + “:ping_param>” + Obj2XML(ping_param,true) + “</” + prefixns + “:ping_param>”;
ss2 += SoapFooter(namefunction,soapVersion);
isping2Running = true;
StartCoroutine(ping2Async2(ss2));
}
private IEnumerator ping2Async2(string ss2) {
string namefunction = “ping2”;
Single soapVersion=1.1f;
byte[ ] bb = System.Text.Encoding.UTF8.GetBytes(ss2);
var headers = header(namefunction,soapVersion);
if (cookie!=“”) {
headers.Add(“Set-Cookie”,cookie);
}
webserviceping2 = new WWW(url, bb, headers);
while( !webserviceping2.isDone ) {
yield return new WaitForSeconds(0.5f);
}
isping2Running = false;
string other = cleanSOAPAnswer(webserviceping2.text, “ping2”,ref ping2Error);
ping2Result=new TmpClase();
ping2Result=(TmpClase)XML2Obj(other,“TmpClase”,ping2Result.GetType());
webserviceping2.responseHeaders.TryGetValue(“SET-COOKIE”,out cookie);
ping2AsyncDone();
}
public void ping2AsyncDone() {
// we do something…
// TmpClase dnx=ping2Result;
}
#region util_function
private Hashtable header(string nameFunction,Single soapVersion) {
var tmpheader=new Hashtable();
if (soapVersion>=1.2) {
tmpheader[“Content-Type”] = “application/soap+xml;charset” + charset + “;action="” + url + “/” + nameFunction +“"”;
} else {
tmpheader[“Content-Type”] = “text/xml;charset” + charset;
tmpheader[“SOAPAction”]= “"”+ tempuri + “/” + nameFunction +“"”;
}
return tmpheader;
}
private string cleanSOAPAnswer(string text, string functionname,ref string last_error )
{
int p0, p1, pbody;
string tmp = functionname + “Result”;
pbody = text.IndexOf(“soap:Body”);
if (pbody<0) {
last_error=“No soap found”;
return “”;
}
p0 = text.IndexOf(“<” + tmp, pbody);
if (p0 <= 0)
{
return “”;
}
p0 = text.IndexOf(“>”, p0) + 1;
p1 = text.IndexOf(“</” + tmp, p0);
if (p1 < p0)
{
tmp = “”;
}
else
{
tmp = text.Substring(p0, p1 - p0);
}
return tmp;
}
private string SoapHeader(string nameFunction,Single soapVersion)
{
string ss2;
if (soapVersion>=1.2) {
ss2 = “<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope\” xmlns:" + prefixns + “="” +
tempuri + “/">”;
ss2 += “soap:Header/soap:Body”;
} else {
ss2 = “<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/\” xmlns:" + prefixns + “="” +
tempuri + “/">”;
ss2 += “soapenv:Header/soapenv:Body”;
}
ss2 += “<” + prefixns + “:” + nameFunction + “>”;
return ss2;
}
private string SoapFooter(string nameFunction,Single soapVersion)
{
string ss2;
ss2 = “</” + prefixns + “:” + nameFunction + “>”;
if (soapVersion>=1.2f) {
ss2 += “</soap:Body></soap:Envelope>”;
} else {
ss2 += “</soapenv:Body></soapenv:Envelope>”;
}
return ss2;
}
private string Obj2XML(object obj)
{
return Obj2XML(obj, false, false);
}
private string Obj2XML(object obj, Boolean withns)
{
return Obj2XML(obj, withns, false);
}
private string Obj2XML(object obj, Boolean withns, Boolean full)
{
string myStr, types;
if (obj.GetType().Namespace == “System”)
{
types = obj.GetType().Name.ToLower();
types = (withns) ? prefixns + “:” + types : types;
if (full)
{
myStr = “<” + types + “>” + obj.ToString() + “</” + types + “>”;
}
else
{
myStr = obj.ToString();
}
}
else
{
var mstream = new MemoryStream();
XmlSerializer SerializerObj = new XmlSerializer(obj.GetType(), tempuri);
if (withns)
{
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(prefixns, tempuri);
SerializerObj.Serialize(mstream, obj, namespaces);
}
else
{
SerializerObj.Serialize(mstream, obj);
}
mstream.Position = 0;
var sreader = new StreamReader(mstream);
myStr = sreader.ReadToEnd();
// cut xml and first node element from the xml
if (!full)
{
var arr = myStr.Split(new char[ ] {‘\n’}, StringSplitOptions.None);
//var arr2 = “”;
int lim = arr.Length - 3;
var arr2 = String.Join(“”, arr, 2, lim);
myStr = arr2;
}
}
return myStr;
}
private object XML2Obj(string xmlstr, string typedesc, Type type) {
return XML2Obj( xmlstr, typedesc,type,false);
}
private object XML2Obj(string xmlstr, string typedesc, Type type,Boolean full)
{
if (!full)
{
xmlstr = “<?xml version=\"1.0\" ?>” + “<” + typedesc + “>” + xmlstr + “</” + typedesc + “>”;
}
var objdummy = new object();
var SerializerObj = new XmlSerializer(type);
byte[ ] byteArray = Encoding.ASCII.GetBytes(xmlstr);
objdummy = SerializerObj.Deserialize(new MemoryStream(byteArray));
return objdummy;
}
#endregion
} // end class.