Beispiel #1
0
        public T GetResponseObject <T>(WemoResponse response)
        {
            if (string.IsNullOrWhiteSpace(response.ResponseBody))
            {
                throw new Exception($"StatusCode: {response.StatusCode}, Description: {response.Description}");
            }

            // Soap parsing
            XNamespace ns  = "http://schemas.xmlsoap.org/soap/envelope/";
            var        doc = XDocument.Parse(response.ResponseBody)
                             .Descendants()
                             .Descendants(ns + "Body").FirstOrDefault()
                             .Descendants().FirstOrDefault();

            // Deserialize to the specific class
            var responseObject = SerializationUtil.Deserialize <T>(doc);

            return(responseObject);
        }
Beispiel #2
0
        public string GetResponseValue(WemoResponse response)
        {
            if (string.IsNullOrWhiteSpace(response.ResponseBody))
            {
                throw new Exception($"StatusCode: {response.StatusCode}, Description: {response.Description}");
            }

            var value = string.Empty;

            // Soap parsing
            XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";

            value = XDocument.Parse(response.ResponseBody)
                    .Descendants()
                    .Descendants(ns + "Body").FirstOrDefault()
                    .Descendants().FirstOrDefault().Value;

            return(value);
        }
Beispiel #3
0
        private static async Task <WemoResponse> ExecuteGetResponseAsync(HttpWebRequest request, string reqContentSoap)
        {
            WemoResponse response;

            // Write the Soap Request to the Request Stream
            using (var requestStream = await request.GetRequestStreamAsync())
            {
                var encoding = new UTF8Encoding();
                requestStream.Write(encoding.GetBytes(reqContentSoap), 0, encoding.GetByteCount(reqContentSoap));
            }

            // Send the Request and acquire the Response
            try
            {
                var httpResponse = await request.GetResponseAsync() as HttpWebResponse;

                using (var rspStm = httpResponse.GetResponseStream())
                {
                    using (var reader = new StreamReader(rspStm))
                    {
                        // Translate the Http Response to our own Response object
                        response = new WemoResponse
                        {
                            Description  = httpResponse.StatusDescription,
                            StatusCode   = httpResponse.StatusCode.ToString(),
                            ResponseBody = reader.ReadToEnd()
                        };
                    }
                }
            }
            catch (WebException ex)
            {
                response = new WemoResponse
                {
                    Description  = $"Exception message: {ex.Message}",
                    StatusCode   = ex.Status.ToString(),
                    ResponseBody = string.Empty
                };
            }

            return(response);
        }