Jump to content
  • 0

How can I connect to SOAP through C # or java


jinsheng peng

Question

2 answers to this question

Recommended Posts

  • 0
23 hours ago, jladd45205 said:

Yes you can connect to the SOAP Endpoints via C#.  Although u cant generate the WSDL, u can manually setup the Envelope XML yourself and send it as a normal web request.  Below is some sample code I used to send money via mail to a specific account.  You just need to look at the source on how the SOAP endpoint is reading the incoming stuff and then reverse engineer it.  


public async Task SendMoney(string charecter, long quanity)
        {
            var realm = await _forumnDB.Realms.FindAsync(_realmID);

            if (!string.IsNullOrWhiteSpace(realm.SoapIP) && realm.SoapPort > 0 && !string.IsNullOrWhiteSpace(realm.SoapUserName) && !string.IsNullOrWhiteSpace(realm.SoapPassword))
            {
                XmlDocument soapEnvelopeXml = GetXMLDoc(string.Format("send money {0} \"Approved GM Request Money\" \"Attached is the money you requested\" {1}\n", charecter, quanity));
                HttpWebRequest webRequest = CreateWebRequest(string.Format("http://{0}:{1}", realm.SoapIP, realm.SoapPort), realm.SoapUserName, realm.SoapPassword);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

                // begin async call to web request.
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

                // suspend this thread until call is complete. You might want to
                // do something usefull here like update your UI.
                asyncResult.AsyncWaitHandle.WaitOne();

                // get the response from the completed web request.
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                    }

                }
            }
            else
            {

            }
        }

        XmlDocument GetXMLDoc(string command)
        {
            XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(string.Format(@"<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/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema"" xmlns:ns1=""urn:MaNGOS""><SOAP-ENV:Body><ns1:executeCommand><command>{0}</command></ns1:executeCommand></SOAP-ENV:Body></SOAP-ENV:Envelope> ", command));
            return soapEnvelopeDocument;
        }

        HttpWebRequest CreateWebRequest(string url, string username, string password)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Credentials = new NetworkCredential(username, password);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }

 

Thank you very much for your reply. I successfully connected to the server.

Link to comment
Share on other sites

  • 0

Yes you can connect to the SOAP Endpoints via C#.  Although u cant generate the WSDL, u can manually setup the Envelope XML yourself and send it as a normal web request.  Below is some sample code I used to send money via mail to a specific account.  You just need to look at the source on how the SOAP endpoint is reading the incoming stuff and then reverse engineer it.  

public async Task SendMoney(string charecter, long quanity)
        {
            var realm = await _forumnDB.Realms.FindAsync(_realmID);

            if (!string.IsNullOrWhiteSpace(realm.SoapIP) && realm.SoapPort > 0 && !string.IsNullOrWhiteSpace(realm.SoapUserName) && !string.IsNullOrWhiteSpace(realm.SoapPassword))
            {
                XmlDocument soapEnvelopeXml = GetXMLDoc(string.Format("send money {0} \"Approved GM Request Money\" \"Attached is the money you requested\" {1}\n", charecter, quanity));
                HttpWebRequest webRequest = CreateWebRequest(string.Format("http://{0}:{1}", realm.SoapIP, realm.SoapPort), realm.SoapUserName, realm.SoapPassword);
                InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

                // begin async call to web request.
                IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

                // suspend this thread until call is complete. You might want to
                // do something usefull here like update your UI.
                asyncResult.AsyncWaitHandle.WaitOne();

                // get the response from the completed web request.
                string soapResult;
                using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                    }

                }
            }
            else
            {

            }
        }

        XmlDocument GetXMLDoc(string command)
        {
            XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(string.Format(@"<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/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema"" xmlns:ns1=""urn:MaNGOS""><SOAP-ENV:Body><ns1:executeCommand><command>{0}</command></ns1:executeCommand></SOAP-ENV:Body></SOAP-ENV:Envelope> ", command));
            return soapEnvelopeDocument;
        }

        HttpWebRequest CreateWebRequest(string url, string username, string password)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Credentials = new NetworkCredential(username, password);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }

 

Edited by jladd45205
  • Like 1
Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use