Accesssing web service within Unity

I’m currently working on a web app that needs to acquire information from an online database. I’m trying to access a SOAP web service with an HTTP POST request using XML. So far I’ve been trying to use WWW and WWWForm class to do this, but I haven’t been able to find any proper examples to guide me. Does anyone have an example or ideas (or some thread that’s already been answered that I did not see) to send an HTTP POST request in properly formed XML within Unity Javascript?

The example of the HTTP POST request format:

POST /Service.asmx HTTP/1.1
Host: [url]www.websitedata.com[/url]
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetData xmlns="www.websitedatacom">
<userToken>string</userToken>
<symbol>string</symbol>
<startDate>dateTime</startDate>
<endDate>dateTime</endDate>
<IncludeHeaderOrSchema>boolean</IncludeHeaderOrSchema>
</GetData>
<?/soap12:Body>
</soap: Envelope>

Any help you can provide would be appreciated as I’m still fairly new to this. Thanks!

I am not at my pc right now but I will post some code when I get in today. The WWW class has an overloaded method to call a URL and allows you to pass in a hashtable of HTTP header values and I think a string or some data bytes. Works perfect with our web services. Again sorry for the lack of solid memory set it up a while ago and haven’t thought much of it since.

Here is en example version of my code. The XML is actually something useful in the real thing and could be replaced with JSON or whatever.

		System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
		Hashtable postHeader = new Hashtable();

		string xmlPostData = "<?xml version=\"1.0\"?>";
		
		postHeader.Add("Content-Type", "application/x-www-form-urlencoded");
		postHeader.Add("Content-Length", xmlPostData.Length);
		postHeader.Add("Connection", "Keep-Alive");
		postHeader.Add("Pragma", "no-cache");
		
		return new WWW("http://somewhere.org", encoding.GetBytes(xmlPostData), postHeader);

Thanks Michael, that helps a lot. We were looking for an example like that, so we can work with that. I’ll post if I have any more issues.

So I got everything working, the code is sending and receiving information from the online database, however, when I built the program to a web player, it did not send out the request, or there was no response (not sure where I would check this at).

So is there a different way to do HTTP POST requests if you’re working inside a web player? or something that needs to be altered? here is a sample of my code for the request, maybe it just involves tweaking something to work outside of the unity editor.

static function GetUserToken ()
{
	var encoding = new System.Text.UTF8Encoding();
	var postHeader = new Hashtable();
	
	var xmlPostData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
	"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 
	  "<soap:Body>" +
	    "<GetUserToken xmlns=\"http://ws.historicaloptiondata.com/\">" +
	      "<LogonId>" + userName + "</LogonId>"+
	      "<Password>" + password + "</Password>" +
	    "</GetUserToken>" +
	  "</soap:Body>" +
	"</soap:Envelope>";
	
	postHeader.Add("Content-Type", "text/xml");
	postHeader.Add("Content-Length", xmlPostData.Length); 
	postHeader.Add("SOAPAction", "http://" + HOSTADDRESS + "/GetUserToken");

	return new WWW(HOSTADDRESS, encoding.GetBytes(xmlPostData), postHeader);
	
}

This manual page explains how to access the log files when using a webplayer.

I talked to a fellow programmer and he mentioned to me where to find it in the Console logs. the error I was receiving was:

You are trying to load data from a www stream which had the following error when downloading.
Failed downloading ws.historicaloptiondata.com
UnityEngine.WWW:get_data()
UnityEngine.WWW:get_data()

not too descriptive. I wasn’t sure if there is some sort of security issues I have to disable to allow data streams or something. or if there is something the website would need to change on their end to allow website streaming data.