Search Unity

Accesssing web service within Unity

Discussion in 'Scripting' started by Grundel, Jan 13, 2010.

  1. Grundel

    Grundel

    Joined:
    Jan 12, 2010
    Posts:
    9
    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:
    Code (csharp):
    1. POST /Service.asmx HTTP/1.1
    2. Host: [url]www.websitedata.com[/url]
    3. Content-Type: application/soap+xml; charset=utf-8
    4. Content-Length: length
    5. <?xml version="1.0" encoding="utf-8"?>
    6. <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">
    7. <soap12:Body>
    8. <GetData xmlns="www.websitedatacom">
    9. <userToken>string</userToken>
    10. <symbol>string</symbol>
    11. <startDate>dateTime</startDate>
    12. <endDate>dateTime</endDate>
    13. <IncludeHeaderOrSchema>boolean</IncludeHeaderOrSchema>
    14. </GetData>
    15. <?/soap12:Body>
    16. </soap: Envelope>
    Any help you can provide would be appreciated as I'm still fairly new to this. Thanks!
     
  2. MichaelDuncan

    MichaelDuncan

    Joined:
    Oct 12, 2009
    Posts:
    12
    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.
     
  3. MichaelDuncan

    MichaelDuncan

    Joined:
    Oct 12, 2009
    Posts:
    12
    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.

    Code (csharp):
    1.         System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    2.         Hashtable postHeader = new Hashtable();
    3.  
    4.         string xmlPostData = "<?xml version=\"1.0\"?>";
    5.        
    6.         postHeader.Add("Content-Type", "application/x-www-form-urlencoded");
    7.         postHeader.Add("Content-Length", xmlPostData.Length);
    8.         postHeader.Add("Connection", "Keep-Alive");
    9.         postHeader.Add("Pragma", "no-cache");
    10.        
    11.         return new WWW("http://somewhere.org", encoding.GetBytes(xmlPostData), postHeader);
    12.  
     
  4. Grundel

    Grundel

    Joined:
    Jan 12, 2010
    Posts:
    9
    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.
     
  5. Grundel

    Grundel

    Joined:
    Jan 12, 2010
    Posts:
    9
    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.

    Code (csharp):
    1. static function GetUserToken ()
    2. {
    3.     var encoding = new System.Text.UTF8Encoding();
    4.     var postHeader = new Hashtable();
    5.    
    6.     var xmlPostData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    7.     "<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/\">" +
    8.       "<soap:Body>" +
    9.         "<GetUserToken xmlns=\"http://ws.historicaloptiondata.com/\">" +
    10.           "<LogonId>" + userName + "</LogonId>"+
    11.           "<Password>" + password + "</Password>" +
    12.         "</GetUserToken>" +
    13.       "</soap:Body>" +
    14.     "</soap:Envelope>";
    15.    
    16.     postHeader.Add("Content-Type", "text/xml");
    17.     postHeader.Add("Content-Length", xmlPostData.Length);
    18.     postHeader.Add("SOAPAction", "http://" + HOSTADDRESS + "/GetUserToken");
    19.  
    20.     return new WWW(HOSTADDRESS, encoding.GetBytes(xmlPostData), postHeader);
    21.    
    22. }
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    This manual page explains how to access the log files when using a webplayer.
     
  7. Grundel

    Grundel

    Joined:
    Jan 12, 2010
    Posts:
    9
    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.