コード例 #1
0
        /// <summary>
        /// This helper function controls the actual formatting of the data
        /// written to the web request stream.
        /// </summary>
        /// <param name="request">A web request instance properly configured for SMP.</param>
        /// <param name="reqicoms">An ICOMS element woith a proper child element to indicate a task.
        /// </param>
        protected virtual void SendRequest(WebRequest webRequest, ITVRequest smpRequest)
        {
            Stream        stream        = null;
            XmlTextWriter xmlTextWriter = null;

            try
            {
                stream        = webRequest.GetRequestStream();
                xmlTextWriter = new XmlTextWriter(stream, this.RequestEncoding);
                xmlTextWriter.WriteRaw(ObjectSerializerWithEncoding.Serialize(smpRequest, Encoding.UTF8));
            }
            catch (WebException webEx)
            {
                throw new SmpUnavailableException(webEx);
            }
            catch (Exception ex)
            {
                throw new SmpProxyException(ex);
            }
            finally
            {
                //clean up writer and stream
                if (null != xmlTextWriter)
                {
                    xmlTextWriter.Close();
                }
                if (null != stream)
                {
                    stream.Close();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// This method reads the response stream and serializes the results
        /// into a proper SMP Response.
        /// </summary>
        /// <param name="response">
        /// A standard web response object obtained from the above web request
        /// instance.
        /// </param>
        /// <returns>
        /// An ITVResponse with the appropriate elements set
        /// </returns>
        protected virtual ITVResponse ReceiveResponse(HttpWebResponse response)
        {
            //check http response before we try to deserialize
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new SmpUnavailableException(
                          string.Format("The call to the WOTL failed with return code '{0}' and message '{1}'", response.StatusCode.ToString(), response.StatusDescription));
            }

            ITVResponse  smpResponse  = null;
            Stream       stream       = null;
            StreamReader streamReader = null;

            try
            {
                stream = response.GetResponseStream();
                // NOTE: Cannot use XmlTextReader. It complains and gives
                // an empty string. instead use the generic StreamReader
                streamReader = new StreamReader(stream);
                smpResponse  = (ITVResponse)ObjectSerializerWithEncoding.Deserialize(streamReader.ReadToEnd(), typeof(ITVResponse));
            }
            catch (IOException ioEx)
            {
                throw new SmpUnavailableException(ioEx);
            }
            catch (InvalidOperationException ivoEx)
            {
                throw new SmpUnavailableException(ivoEx);
            }
            catch (Exception ex)
            {
                throw new SmpProxyException(ex);
            }
            finally
            {
                //clean up stream reader and stream
                if (null != streamReader)
                {
                    streamReader.Close();
                }
                if (null != stream)
                {
                    stream.Close();
                }
            }
            return(smpResponse);
        }