コード例 #1
0
        // Asynchronous method to get request stream
        private Task <Stream> GetRequestStream(HttpWebRequest request)
        {
            Stream requestStream;
            var    taskComplete = new TaskCompletionSource <Stream>();

            request.BeginGetRequestStream(asyncRequest =>
            {
                try
                {
                    HttpWebRequest responseRequest = (HttpWebRequest)asyncRequest.AsyncState;
                    requestStream = (Stream)responseRequest.EndGetRequestStream(asyncRequest);
                    taskComplete.TrySetResult(requestStream);
                }
                catch (WebException ex)
                {
                    if (OnErrorReported != null)
                    {
                        UcwaAppUtils.ReportError(OnErrorReported, ex);
                    }
                }
            }, request);
            return(taskComplete.Task);
        }
コード例 #2
0
        public async Task <HttpStatusCode> SignIn(string userName, string password)
        {
            this.userName           = userName;
            this.password           = password;
            this.authenticationType = AuthenticationTypes.Password;
            try
            {
                var opResult = await DiscoverRootResource(this.discoverFromInternalDomain);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "GetRootResource returns null result.", opResult.HttpStatusCode);
                    return(opResult.HttpStatusCode);
                }

                opResult = await GetUserResource(opResult.Resource.GetLinkUri("user"), userName, password, this.authenticationType);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported,
                                                userName + " cannot be authenticated, with the " + this.authenticationType.ToString() + " grant_type.",
                                                opResult.HttpStatusCode);
                    return(opResult.HttpStatusCode);
                }
                // Create the UCWA application bound to the specified user
                opResult = await GetApplicationResource(opResult.Resource);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to create the UCWA application resource.", opResult.HttpStatusCode);
                    return(opResult.HttpStatusCode);
                }

                this.ApplicationResource = opResult.Resource;
                UcwaAppUtils.ReportProgress(OnProgressReported, "Succeded in creating the application resource: " + this.ApplicationResource.Uri);


                // Make me available to receive incoming alerts
                this.Me = new UcwaAppMe(this);
                var statusCode = await this.Me.PostMakeMeAvailable("4255552222", "Online",
                                                                   new string[] { "Plain", "Html" }, new string[] { "PhoneAudio", "Messaging" });

                if (statusCode != HttpStatusCode.NoContent)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to post to makeMeAvailable resource.", statusCode);
                    return(statusCode);
                }

                // Get application resource again to receive any updates triggered by the POST request to making me available
                opResult = await GetApplicationResource(this.ApplicationResource.Uri);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to get the updated application resource", opResult.HttpStatusCode);
                    return(opResult.HttpStatusCode);
                }
                this.ApplicationResource = opResult.Resource;
                statusCode = await this.Me.Refresh();
            }
            catch (Exception ex)
            {
                UcwaAppUtils.ReportError(OnErrorReported, ex);
                return(HttpStatusCode.BadRequest);
            }
            return(HttpStatusCode.OK);
        }