Exemple #1
0
        protected override async Task <Stream> SendRemoteRequestInternalAsync()
        {
            IHttpWebRequest httpWebRequest = httpWebRequestFactory.Create("https://visualstudio-devdiv-c2s.msedge.net/ab");

            httpWebRequest.Method      = "GET";
            httpWebRequest.CachePolicy = DefaultCachePolicy;
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.AddHeaders(GetAllFilters());
            Stream stream = null;

            try
            {
                CancellationTokenSource tokenSource = new CancellationTokenSource();
                Task.Delay(60000).ContinueWith(delegate
                {
                    tokenSource.Cancel();
                });
                IHttpWebResponse httpWebResponse = await httpWebRequest.GetResponseAsync(tokenSource.Token).ConfigureAwait(false);

                if (httpWebResponse.ErrorCode != 0)
                {
                    return(stream);
                }
                stream = httpWebResponse.GetResponseStream();
                return(stream);
            }
            catch
            {
                return(stream);
            }
        }
Exemple #2
0
        /// <summary>
        /// Execute request and returns entityResponse - ASYNC.
        /// </summary>
        /// <param name="httpWebRequest">Http web request.</param>
        /// <returns></returns>
        protected async Task <IHttpWebResponse> ExecuteRequestAsync(IHttpWebRequest httpWebRequest)
        {
            this.PreExecuteRequest(httpWebRequest);
            IHttpWebResponse httpWebResponse = await httpWebRequest.GetResponseAsync();

            this.ProcessHttpWebResponseInternal(httpWebResponse);
            return(httpWebResponse);
        }
        private async Task <string> ResolveUrlRedirect(string url, CancellationToken token)
        {
            IHttpWebRequest httpWebRequest = webRequestFactory.Create(url);

            httpWebRequest.Method            = "HEAD";
            httpWebRequest.AllowAutoRedirect = false;
            IHttpWebResponse httpWebResponse = await httpWebRequest.GetResponseAsync(token).ConfigureAwait(false);

            if (httpWebResponse.ErrorCode == ErrorCode.NoError && httpWebResponse.Headers != null)
            {
                string text = httpWebResponse.Headers["Location"];
                if (text != null)
                {
                    return(text);
                }
                throw new TargetedNotificationsException("Could not resolve url redirect (no location header was received)");
            }
            throw new TargetedNotificationsException($"Could not resolve url redirect ({httpWebResponse.ErrorCode}-{httpWebResponse.StatusCode}-{httpWebResponse.ExceptionCode})");
        }
        private async Task <Stream> SendTargetedNotificationsRequestAsync(IEnumerable <string> previouslyCachedRuleIds)
        {
            Stream stream = null;

            apiTimer = Stopwatch.StartNew();
            try
            {
                CancellationTokenSource tokenSource = new CancellationTokenSource();
                Task.Delay(10000).ContinueWith(delegate
                {
                    tokenSource.Cancel();
                });
                CancellationToken cancelToken = CancellationTokenSource.CreateLinkedTokenSource(tokenSource.Token, cancellationTokenSource.Token).Token;
                string            parameters  = await BuildRequestParametersAsync(previouslyCachedRuleIds).ConfigureAwait(false);

                IHttpWebRequestFactory httpWebRequestFactory = webRequestFactory;
                IHttpWebRequest        request = httpWebRequestFactory.Create(await ResolveUrlRedirect("https://go.microsoft.com/fwlink/?LinkId=690387", cancelToken).ConfigureAwait(false));
                request.ContentType = "application/json";
                request.Method      = "POST";
                logger.LogVerbose("Sending request to TN backend", parameters);
                byte[] postData = Encoding.UTF8.GetBytes(parameters);
                request.ContentLength = postData.Length;
                Stream postStream = await request.GetRequestStreamAsync().ConfigureAwait(false);

                await postStream.WriteAsync(postData, 0, postData.Length, cancelToken).ConfigureAwait(false);

                postStream.Close();
                IHttpWebResponse httpWebResponse = await request.GetResponseAsync(cancelToken).ConfigureAwait(false);

                apiTimer.Stop();
                if (httpWebResponse.ErrorCode == ErrorCode.NoError)
                {
                    stream = httpWebResponse.GetResponseStream();
                }
                else
                {
                    string eventName   = "VS/Core/TargetedNotifications/ApiRequestFailed";
                    string description = "Request failed.";
                    Dictionary <string, object> dictionary = new Dictionary <string, object>
                    {
                        {
                            "VS.Core.TargetedNotifications.ErrorCode",
                            httpWebResponse.ErrorCode
                        },
                        {
                            "VS.Core.TargetedNotifications.ApiResponseMs",
                            apiTimer?.ElapsedMilliseconds
                        },
                        {
                            "VS.Core.TargetedNotifications.Iteration",
                            queryIteration
                        }
                    };
                    if (httpWebResponse.ErrorCode == ErrorCode.WebExceptionThrown)
                    {
                        dictionary.Add("VS.Core.TargetedNotifications.WebExceptionCode", httpWebResponse.ExceptionCode);
                    }
                    targetedNotificationsTelemetry.PostDiagnosticFault(eventName, description, null, dictionary);
                }
                return(stream);
            }
            catch (Exception exception)
            {
                apiTimer.Stop();
                string eventName2 = "VS/Core/TargetedNotifications/ApiRequestException";
                string text       = "Sending request to TN backend failed";
                Dictionary <string, object> additionalProperties = new Dictionary <string, object>
                {
                    {
                        "VS.Core.TargetedNotifications.ApiResponseMs",
                        apiTimer?.ElapsedMilliseconds
                    },
                    {
                        "VS.Core.TargetedNotifications.Iteration",
                        queryIteration
                    }
                };
                logger.LogError(text, exception);
                targetedNotificationsTelemetry.PostDiagnosticFault(eventName2, text, exception, additionalProperties);
                return(stream);
            }
        }