Esempio n. 1
0
        /// <summary>
        /// Create a <see cref="WebException"/> with the specified content.
        /// </summary>
        /// <param name="status">The status code to use in the exception.</param>
        /// <param name="content">A <see cref="MemoryStream"/> of the exception content.</param>
        /// <param name="contextHandler">An action that adds extra info to the response.</param>
        /// <returns>An <see cref="WebException"/> with the specified content.</returns>
        public static WebException CreateWebException(
            HttpStatusCode status,
            MemoryStream content,
            Action <HttpListenerContext> contextHandler)
        {
            HttpListener server = null;

            try
            {
                // Create a mock server that always returns the response code and exception stream
                // specified in the parameter.
                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    MockHttpServer mockServer = new MockHttpServer(
                        exceptionManager,
                        MockHttpServer.DefaultServerPrefixUri);
                    server = mockServer.CreateListener(
                        (context) =>
                    {
                        contextHandler(context);
                        context.Response.StatusCode = (int)status;
                        content.Position            = 0;
                        content.CopyTo(context.Response.OutputStream);
                        context.Response.Close();
                    },
                        1);
                }

                WebClient client = new WebClient();
                try
                {
                    client.OpenRead(new Uri(DefaultServerPrefixUri, "exception.htm"));
                }
                catch (WebException ex)
                {
                    return(ex);
                }
            }
            finally
            {
                server.Stop();
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a <see cref="WebException"/> with the specified content.
        /// </summary>
        /// <param name="status">The status code to use in the exception.</param>
        /// <param name="content">A <see cref="MemoryStream"/> of the exception content.</param>
        /// <param name="contextHandler">An action that adds extra info to the response.</param>
        /// <returns>An <see cref="WebException"/> with the specified content.</returns>
        public static WebException CreateWebException(
            HttpStatusCode status,
            MemoryStream content,
            Action<HttpListenerContext> contextHandler)
        {
            HttpListener server = null;
            try
            {
                // Create a mock server that always returns the response code and exception stream
                // specified in the parameter.
                using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager())
                {
                    MockHttpServer mockServer = new MockHttpServer(
                        exceptionManager,
                        MockHttpServer.DefaultServerPrefixUri);
                    server = mockServer.CreateListener(
                        (context) =>
                        {
                            contextHandler(context);
                            context.Response.StatusCode = (int)status;
                            content.Position = 0;
                            content.CopyTo(context.Response.OutputStream);
                            context.Response.Close();
                        },
                        1);
                }

                WebClient client = new WebClient();
                try
                {
                    client.OpenRead(new Uri(DefaultServerPrefixUri, "exception.htm"));
                }
                catch (WebException ex)
                {
                    return ex;
                }
            }
            finally
            {
                server.Stop();
            }

            return null;
        }