[C#/.Net] TrinityCore SOAP Client

Hi,

Today I share with you a little C# class I have made to handle SOAP request for TrinityCore SOAP API.

You cane use it in your C# / .net Apps to send real-time commands (.announce, .additems, .senditems etc…) to the server.

using System;
/*

  • Copyright (C) 2014 Elmsroth http://www.rakz.net/
  • This program is free software; you can redistribute it and/or modify it
  • under the terms of the GNU General Public License as published by the
  • Free Software Foundation; either version 2 of the License, or (at your
  • option) any later version.
  • This program is distributed in the hope that it will be useful, but WITHOUT
  • ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  • FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  • more details.
  • You should have received a copy of the GNU General Public License along
  • with this program. If not, see http://www.gnu.org/licenses/.
  • This program is free software; you can redistribute it and/or modify it
  • under the terms of the GNU General Public License as published by the
  • Free Software Foundation; either version 2 of the License, or (at your
  • option) any later version.
  • This program is distributed in the hope that it will be useful, but WITHOUT
  • ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  • FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  • more details.
  • You should have received a copy of the GNU General Public License along
  • with this program. If not, see http://www.gnu.org/licenses/.
    */
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;

/*

  • Special class to handle SOAP Calls to TrinityCore Server
    */
    namespace TrinitySOAP
    {
    public class TrinityCoreSOAPServiceClient
    {
    public int PortNumber { get; set; }
    public string HostOrIp { get; set; }
    public string AccountName { get; set; }
    public string AccountPassword { get; set; }

     public TrinityCoreSOAPServiceClient(string hostOrIp, int portNumber, string accountName, string accountPassword)
     {
         PortNumber = portNumber;
         HostOrIp = hostOrIp;
         AccountName = accountName;
         AccountPassword = accountPassword;
     }
    
     /*
      * Returns the result of the command
      */
     public string Call(string command)
     {
         String requestBody = "<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:TC"><SOAP-ENV:Body><ns1:executeCommand><command>"+command+"</command></ns1:executeCommand></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    
         var request = (HttpWebRequest)WebRequest.Create(string.Format("http://{0}:{1}", HostOrIp, PortNumber));
    
         var data = Encoding.UTF8.GetBytes(requestBody);
         request.Method = "POST";
         request.ContentType = "text/xml";
         request.ContentLength = data.Length;
         request.Accept ="text/xml";
         request.Headers.Add("SOAPAction: executeCommand");
         var encodedPasswd = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", AccountName, AccountPassword));
         request.Headers.Add("Authorization: Basic " + Convert.ToBase64String(encodedPasswd));
    
         using (var stream = request.GetRequestStream())
         {
             stream.Write(data, 0, data.Length);
         }
    
         var response = (HttpWebResponse)request.GetResponse();
         var rsStream = response.GetResponseStream();
    
         return ExtractResponseStringFromSOAPResponse(new StreamReader(rsStream).ReadToEnd());
     }
    
     private string ExtractResponseStringFromSOAPResponse(string xmlSOAPresponse)
     {
         XmlDocument xml = new XmlDocument();
         xml.LoadXml(xmlSOAPresponse);
         XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
         nsmgr.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
         nsmgr.AddNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
         nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
         nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
         nsmgr.AddNamespace("ns1", "urn:TC");
    
         // Select a specific node
         XmlNode node = xml.SelectSingleNode("SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:executeCommandResponse/result", nsmgr);
    
         return node.InnerText;
     }
    

    }
    }

Usage example : (WPF app)

private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Send command
            string commandText = CommandInput.Text;

            var client = new TrinityCoreSOAPServiceClient("127.0.0.1",7878,"dev", "dev");
            var result = client.Call(commandText);
            MessageBox.Show(result);
        }

thanks very much for this, I was able to adapt it to work with my python/flask site where everything else had been too confusing for me

it is possible to convert that in Java?

why not?