示例#1
0
        protected async Task <HttpResponseMessage> GetHttpResponseAsync(string url, int retry = 1)
        {
            DebugHelper.Debugger.WriteDebugLog("GetHttpResponseAsync called. Request URL=" + url + " Available retry=" + retry + ".");
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken);
                var response = await client.GetAsync(url);

                if (!response.IsSuccessStatusCode)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        DebugHelper.Debugger.WriteDebugLog("GetHttpResponseAsync failed. Request URL=" + response.RequestMessage.RequestUri.AbsoluteUri + " HTTP Status=" + response.StatusCode + ". Elapsed time=" + stopwatch.Elapsed.TotalSeconds + "secs.");
                        return(null);
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        if (retry > 0)
                        {
                            await token.AcquireNewToken();

                            return(await GetHttpResponseAsync(url, retry - 1));
                        }
                        else
                        {
                            throw new UnauthorizedAccessException("GetHttpResponseAsync failed. Request URL=" + response.RequestMessage.RequestUri.AbsoluteUri + " HTTP Status=" + response.StatusCode + ". Elapsed time=" + stopwatch.Elapsed.TotalSeconds + "secs.");
                        }
                    }

                    throw new HttpRequestException("GetHttpResponseAsync failed.Request URL=" + response.RequestMessage.RequestUri.AbsoluteUri + ". HTTP Status=" + response.StatusCode + ". Reason=" + response.ReasonPhrase + ". Elapsed time=" + stopwatch.Elapsed.TotalSeconds + "secs.");
                }

                DebugHelper.Debugger.WriteDebugLog("GetHttpResponseAsync succeeded. Request URL=" + response.RequestMessage.RequestUri.AbsoluteUri + " HTTP Status=" + response.StatusCode + ". Elapsed time=" + stopwatch.Elapsed.TotalSeconds + "secs.");
                return(response);
            }
        }
示例#2
0
        private static async Task acquireAndAddUser(List <UserBase> userList, UserType type, Windows.Security.Credentials.PasswordCredential cred)
        {
            var token = new TokenBase();

            token.UserType = type;
            token.Id       = cred.UserName;
            if (type == UserType.Google)
            {
                token.RefreshToken = cred.Password;
                token = await token.AcquireNewToken();
            }
            else
            {
                token.AccessToken = cred.Password;
            }
            userList.Add(await UserBase.AcquireUserFromToken(token));
        }