Esempio n. 1
0
        internal static HttpFormResponse ReadResponse(WebRequest request)
        {
            const int MaxTry = 3;

            int tryCount         = 0;
            var httpFormResponse = new HttpFormResponse();

            // Sometimes the external site can throw exception so we might
            // have to retry few more times
            while (string.IsNullOrEmpty(httpFormResponse.Response) && (tryCount < MaxTry))
            {
                try
                {
                    using (WebResponse response = request.GetResponse())
                    {
                        PopulateHeadersAndCookies(response, httpFormResponse);

                        using (var sr = new StreamReader(response.GetResponseStream()))
                        {
                            httpFormResponse.Response = sr.ReadToEnd();
                        }
                    }
                }
                catch (WebException ex)
                {
                    Log.Warning("ReadResponse: {0}, try: {1}", ex.ToString(), tryCount);
                    tryCount += 1;
                    Thread.Sleep(200);
                }
            }

            return(httpFormResponse);
        }
Esempio n. 2
0
        internal static void PopulateHeadersAndCookies(WebResponse webResponse, HttpFormResponse response)
        {
            response.Headers.Add(webResponse.Headers);

            var httpWebResponse = webResponse as HttpWebResponse;

            if (httpWebResponse != null)
            {
                foreach (Cookie cookie in httpWebResponse.Cookies)
                {
                    response.Cookies.Add(cookie.Name, cookie.Value);
                }
            }
        }
Esempio n. 3
0
        internal static void ResponseCallback(IAsyncResult result)
        {
            var states = (StateContainer)result.AsyncState;

            try
            {
                var         httpFormResponse = new HttpFormResponse();
                WebResponse response         = states.Request.EndGetResponse(result);

                PopulateHeadersAndCookies(response, httpFormResponse);

                using (Stream stream = response.GetResponseStream())
                {
                    const int BufferLength = 8096;
                    var       buffer       = new byte[BufferLength];

                    using (var ms = new MemoryStream())
                    {
                        int bytesRead;

                        while ((bytesRead = stream.Read(buffer, 0, BufferLength)) > 0)
                        {
                            ms.Write(buffer, 0, bytesRead);
                        }

                        ms.Flush();

                        httpFormResponse.Response = Encoding.UTF8.GetString(ms.ToArray());
                    }

                    if (states.OnComplete != null)
                    {
                        states.OnComplete(httpFormResponse);
                    }
                }
            }
            catch (Exception e)
            {
                if (states.OnError != null)
                {
                    states.OnError(e);
                }
            }
        }