Example #1
0
        /// <summary>
        /// Processes the response and sends it off to the caller.
        /// </summary>
        /// <param name="asyncResult">The async result.</param>
        private void ResponseCallback(IAsyncResult asyncResult)
        {
            SendPostEventArgs args = (SendPostEventArgs)asyncResult.AsyncState;

            try
            {
                args.WebResponse = args.WebRequest.EndGetResponse(asyncResult);
            }
            catch (Exception e)
            {
                InvokeResponseCallback(args, e.ToString());
                return;
            }

            Stream responseStream = args.WebResponse.GetResponseStream();

            using (StreamReader reader = new StreamReader(responseStream))
            {
                args.ResponseData = reader.ReadToEnd();
            }

            if (RequestResponseLogEnabled)
            {
                _requestResponseLog.Add(args.ResponseData);
            }

            InvokeResponseCallback(args, null);
        }
Example #2
0
        /// <summary>
        /// Handles the callback after sending a request.
        /// </summary>
        /// <param name="sender">The sender of the notification.</param>
        /// <param name="args">The event args.</param>
        private void SendRequestCallback(object sender, SendPostEventArgs args)
        {
            if (args.ErrorText != null)
            {
                string errorText = "Error in talking to HealthVault: " + args.ErrorText;
                InvokeApplicationResponseCallback(args.HealthVaultRequest, null, null, errorText);
                return;
            }

            HealthVaultResponse response = null;

            try
            {
                response = new HealthVaultResponse(args.ResponseData);
            }
            catch (InvalidOperationException)
            {
                string errorText = "Response was not a valid HealthVault response: " + args.ResponseData;
                InvokeApplicationResponseCallback(args.HealthVaultRequest, null, null, errorText);
                return;
            }

            // The token that is returned from GetAuthenticatedSessionToken has a limited lifetime. When it expires,
            // we will get an error here. We detect that situation, get a new token, and then re-issue the call.
            if (response.StatusCode == AuthenticatedSessionTokenExpired)
            {
                RefreshSessionToken(args);
                return;
            }

            InvokeApplicationResponseCallback(args.HealthVaultRequest, args.ResponseData, response, response.ErrorMessage);
        }
Example #3
0
        /// <summary>
        /// Refresh the session token.
        /// </summary>
        /// <remarks>Makes a CAST call to get a new session token,
        /// and then re-issues the original request.</remarks>
        /// <param name="args">The request information.</param>
        private void RefreshSessionToken(SendPostEventArgs args)
        {
            AuthorizationSessionToken = null;

            XElement           info    = CreateCastCallInfoSection();
            HealthVaultRequest request = HealthVaultRequest.Create("CreateAuthenticatedSessionToken", "2", info, RefreshSessionTokenCompleted);

            request.UserState = args;

            BeginSendRequest(request);
        }
Example #4
0
        /// <summary>
        /// Call the user's callback handler.
        /// </summary>
        /// <param name="args">The arguments to pass.</param>
        /// <param name="errorText">The error text.</param>
        private void InvokeResponseCallback(
            SendPostEventArgs args,
            string errorText)
        {
            args.ErrorText = errorText;

            if (args.ResponseCallback != null)
            {
                args.ResponseCallback(this, args);
            }
        }
Example #5
0
        /// <summary>
        /// Sends the request post data.
        /// </summary>
        /// <param name="asyncResult">The async result.</param>
        private void GetRequestStreamCallback(IAsyncResult asyncResult)
        {
            SendPostEventArgs args = (SendPostEventArgs)asyncResult.AsyncState;

            using (Stream postStream = args.WebRequest.EndGetRequestStream(asyncResult))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(args.RequestData);
                postStream.Write(bytes, 0, bytes.Length);
            }

            args.WebRequest.BeginGetResponse(ResponseCallback, args);
        }
Example #6
0
        /// <summary>
        /// Processes the CAST information and re-issues the original request.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args.</param>
        private void RefreshSessionTokenCompleted(object sender, HealthVaultResponseEventArgs e)
        {
            SendPostEventArgs args = (SendPostEventArgs)e.Request.UserState;

            // any error just gets returned to the application.
            if (e.ErrorText != null)
            {
                InvokeApplicationResponseCallback(args.HealthVaultRequest, args.ResponseData, e.Response, e.Response.ErrorMessage);
                return;
            }

            // if the CAST was successful the results were saved and
            // the original request is restarted.
            SaveCastCallResults(e.ResponseXml);

            BeginSendRequest(args.HealthVaultRequest);
        }
Example #7
0
        /// <summary>
        /// Begin to send a post request to a specific url.
        /// </summary>
        /// <param name="url">The target url.</param>
        /// <param name="requestData">The data to post to the url.</param>
        /// <param name="responseCallback">The completion routine.</param>
        /// <param name="userRequest">User parameter.</param>
        public virtual void BeginSendPostRequest(
            string url,
            string requestData,
            EventHandler <SendPostEventArgs> responseCallback,
            HealthVaultRequest userRequest)
        {
            if (RequestResponseLogEnabled)
            {
                _requestResponseLog.Add(requestData);
            }

            SendPostEventArgs args = new SendPostEventArgs();

            args.RequestData        = requestData;
            args.HealthVaultRequest = userRequest;
            args.ResponseCallback   = responseCallback;

            if (userRequest.WebRequest == null)
            {
                args.WebRequest        = WebRequest.Create(url);
                args.WebRequest.Method = "POST";
            }
            else
            {
                // Mock case...
                args.WebRequest = userRequest.WebRequest;
            }

            try
            {
                IAsyncResult result = args.WebRequest.BeginGetRequestStream(GetRequestStreamCallback, args);
            }
            catch (Exception e)
            {
                InvokeResponseCallback(args, e.ToString());
                return;
            }
        }
        /// <summary>
        /// Begin to send a post request to a specific url. 
        /// </summary>
        /// <param name="url">The target url.</param>
        /// <param name="requestData">The data to post to the url.</param>
        /// <param name="responseCallback">The completion routine.</param>
        /// <param name="userRequest">User parameter.</param>
        public virtual void BeginSendPostRequest(
            string url,
            string requestData,
            EventHandler<SendPostEventArgs> responseCallback,
            HealthVaultRequest userRequest)
        {
            if (RequestResponseLogEnabled)
            {
                _requestResponseLog.Add(requestData);
            }
          
            SendPostEventArgs args = new SendPostEventArgs();
            args.RequestData = requestData;
            args.HealthVaultRequest = userRequest;
            args.ResponseCallback = responseCallback;

            if (userRequest.WebRequest == null)
            {
                args.WebRequest = WebRequest.Create(url);
                args.WebRequest.Method = "POST";
            }
            else
            {
                    // Mock case...
                args.WebRequest = userRequest.WebRequest;
            }

            try
            {
                IAsyncResult result = args.WebRequest.BeginGetRequestStream(GetRequestStreamCallback, args);
            }
            catch (Exception e)
            {
                InvokeResponseCallback(args, e.ToString());
                return;
            }
        }
        /// <summary>
        /// Call the user's callback handler.
        /// </summary>
        /// <param name="args">The arguments to pass.</param>
        /// <param name="errorText">The error text.</param>
        private void InvokeResponseCallback(
            SendPostEventArgs args,
            string errorText)
        {
            args.ErrorText = errorText;

            if (args.ResponseCallback != null)
            {
                args.ResponseCallback(this, args);
            }
        }