Change font size
It is currently Mon Nov 19, 2018 11:23 am

Forum rules


{L_IMAGE}



Post a new topicPost a reply Page 1 of 1   [ 2 posts ]
Author Message
 Post subject: Example MRM for HTTPRequest.
PostPosted: Thu Nov 12, 2009 10:39 am 
Furious Typer
User avatar

Joined: Thu Nov 20, 2008 6:22 am
Posts: 126
Here is an example for an MRM-Script making a HTTP Get Request to get the current time from a webservice ( In this case for London ). Still a bit ugly because it dod not filter out the XML stuff. I still have to work on my c# skills.. :lol:

{L_CODE}:
//MRM:C#
/*=============================================================
(c) all rights reserved
================================================================*/
using System.IO;
using System.Net;
using System.Text;
using OpenSim.Region.OptionalModules.Scripting.Minimodule;
using OpenMetaverse;

namespace OpenSim{

   class MiniModule : MRMBase
   {
     
     
   
      public override void Start()
        {   
            Host.Object.OnTouch += OnTouched;
            Host.Object.Say("MRM ready.)");
             
        }
   
   
      void OnTouched(IObject sender, TouchEventArgs e)
        {
           
           try
            {
                StringBuilder sb = new StringBuilder();

                byte[] buf = new byte[8192];
                HttpWebRequest request =
                    (HttpWebRequest) WebRequest.Create("http://www.nanonull.com/TimeService/TimeService.asmx/getCityTime?city=London");
                HttpWebResponse response = (HttpWebResponse) request.GetResponse();

                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    count = resStream.Read(buf, 0, buf.Length);
                    if (count != 0)
                    {
                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                        sb.Append(tempString);
                    }
                } while (count > 0);
               
                Host.Object.Say(sb.ToString());
               
            }
            catch (WebException)
            {
                Host.Object.Say("WebException occured");
            }
                                                               
        }

         
   
      public override void Stop()
        {

        }   
     
   }
   
}



regards

Ziah


Top
 Profile  
 
 Post subject: Re: Example MRM for HTTPRequest.
PostPosted: Fri Nov 20, 2009 10:49 am 
Furious Typer
User avatar

Joined: Thu Nov 20, 2008 6:22 am
Posts: 126
ok, here is the cleaner version, using the System.Xml to load and parse the webservice response :

{L_CODE}:

//MRM:C#

using System.Xml;
using OpenSim.Region.OptionalModules.Scripting.Minimodule;
using OpenMetaverse;

namespace OpenSim{

   class MiniModule : MRMBase
   {
   
      public override void Start()
        {   
            Host.Object.OnTouch += OnTouched;
            Host.Object.Say("(MRM) Script ready."); 
        }
   
      void OnTouched(IObject sender, TouchEventArgs e)
        {
           
           try
            {
                // Retrieve XML document 
                XmlTextReader reader = new XmlTextReader("http://www.nanonull.com/TimeService/TimeService.asmx/getCityTime?city=London"); 
 
                // Skip non-significant whitespace 
                reader.WhitespaceHandling = WhitespaceHandling.Significant; 
                reader.MoveToContent();
               
                Host.Object.Say(reader.ReadElementString("string"));
               
            }
            catch (XmlException)
            {
                Host.Object.Say("Exception occured");
            }
                                             
        }

      public override void Stop()
        {

        }   
   
   }
   
}


regards

Ziah


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 2 posts ]


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
610nm Style by Daniel St. Jules of Gamexe.net