Пример #1
0
        private async Task <IHttpWebResponse> GetResponseAsync(WebRequest rawRequest)
        {
            var timer = new Timer(_config.Advanced.RequestTimeout.TotalMilliseconds);

            timer.Elapsed += (sender, args) => rawRequest.Abort();
            try
            {
                timer.Start();
                var response =
                    await Task <WebResponse> .Factory.FromAsync(rawRequest.BeginGetResponse, rawRequest.EndGetResponse, rawRequest);

                return(new HttpWebResponse((System.Net.HttpWebResponse)response));
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.RequestCanceled)
                {
                    var timeoutResponse = new HttpWebResponse
                    {
                        ContentType = ContentTypes.TextPlain
                    };
                    timeoutResponse.SetResponseStream(new MemoryStream(Encoding.UTF8.GetBytes(string.Format(_timeOutResponse, rawRequest.RequestUri.AbsolutePath, _config.Advanced.RequestTimeout.TotalMilliseconds))));
                    return(timeoutResponse);
                }
                return(new HttpWebResponse((System.Net.HttpWebResponse)e.Response));
            }
            finally
            {
                timer.Close();
                timer.Stop();
                timer.Dispose();
            }
        }
 public void Setup()
 {
     _supported = new List <ContentType> {
         ContentType.ApplicationXml, ContentType.ApplicationJson
     };
     _negotiator = new ContentNegotiator(_supported);
     _response   = new HttpWebResponse
     {
         Headers = new WebHeaderCollection
         {
             { HttpRequestHeader.Accept, "application/json" }
         },
         StatusCode = HttpStatusCode.UnsupportedMediaType
     };
 }