wszystkie wpisy

.NET Client consuming PHP ZendFramework Web Service - solution no.2 (HttpWebRequest)

Dostałem za zadanie zintegrowanie się z kilkoma web serwisami. Naturalnym rozwiązaniem było wybranie do tego celu technologii WPF, co opisałem we wcześniejszym wpisie. Na szczęście, platforma .NET pozwala wykonywać wiele zdań na wiele, różnorodnych sposobów. W tym wpisie, przedstawię inny sposób odpytania web serwisu, tj. za pomocą klasy HttpWebRequest

W przypadku korzystania z klasy HttpWebRequest, całość zapytania SOAP, jakie wykonamy musimy napisać samemu w stringu, a nast. odpytać serwis za pomocą metody POST.

Odpowiedź dostajemy w strumieniu bajtów, który następnie zamieniamy na XmlDocument

public string GetMigDZiDByPost()

        {

            try

            {

                WebRequest webRequest = WebRequest.Create(“http://xxx.yyy.zzz/Dzsoapserver/queryko”);

                HttpWebRequest httpRequest = (HttpWebRequest)webRequest;

                httpRequest.Method = “POST”;

                httpRequest.ContentType = “text/xml”;

                httpRequest.Headers.Add(“SOAPAction: http://xxx.yyy.zzz/Dzsoapserver/queryko”);

                Stream requestStream = httpRequest.GetRequestStream();

                //Create Stream and Complete Request

                StreamWriter streamWriter = new StreamWriter(requestStream);

                streamWriter.Write(this.GetSoapString());

                streamWriter.Close();

                //Get the Response

                WebResponse webResponse = httpRequest.GetResponse();

                Stream responseStream = webResponse.GetResponseStream();

                StreamReader streamReader = new StreamReader(responseStream);

                //Read the response into an xml document

                System.Xml.XmlDocument soapResonseXMLDocument = new System.Xml.XmlDocument();

                soapResonseXMLDocument.LoadXml(streamReader.ReadToEnd());

                //return only the xml representing the response details (inner request)

                string test = soapResonseXMLDocument.GetElementsByTagName(“organisation”)[0].InnerXml;

                return test;

            }

            catch (Exception ex)

            {

                //error 500 mean rekord not found

                return null;

            }

        }

        private string GetSoapString()

        {

            string hash = Constraints.MIGDZ.MigDzHashBase64;

            StringBuilder sb = new StringBuilder();

            sb.Append("");

            sb.Append(“<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=“http://schemas.xmlsoap.org/soap/encoding/\” 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=“http://xxx.yyy.zzz/Dzsoapserver/queryko\” xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/\“>SOAP-ENV:Body”);

            sb.Append(“ns1:getParcel<parameters xsi:type=“ns1:Logic_DzSoapServer_Request_BodyKo”><sy xsi:type=“xsd:string”>12345678<hashCode xsi:type=“xsd:string”>abcdefg0123456VUZ123ABCD01a=</ns1:getParcel></SOAP-ENV:Body></SOAP-ENV:Envelope>”);

            return sb.ToString();

        }