public static async Task Main(string[] args)
        {
            bool tracingDisabled = args.Any(arg => arg.Equals("TracingDisabled", StringComparison.OrdinalIgnoreCase));

            Console.WriteLine($"TracingDisabled {tracingDisabled}");

            string port = args.FirstOrDefault(arg => arg.StartsWith("Port="))?.Split('=')[1] ?? "9000";

            Console.WriteLine($"Port {port}");

            Url = $"http://localhost:{port}/Samples.HttpMessageHandler/";

            Console.WriteLine();
            Console.WriteLine($"Starting HTTP listener at {Url}");

            using (var listener = new HttpListener())
            {
                listener.Prefixes.Add(Url);
                listener.Start();

                // handle http requests in a background thread
                var listenerThread = new Thread(HandleHttpRequests);
                listenerThread.Start(listener);

                // send http requests using HttpClient
                Console.WriteLine();
                Console.WriteLine("Sending request with HttpClient.");
                await RequestHelpers.SendHttpClientRequestsAsync(tracingDisabled, Url, RequestContent);

                Console.WriteLine();
                Console.WriteLine("Stopping HTTP listener.");
                listener.Stop();
            }

            // Force process to end, otherwise the background listener thread lives forever in .NET Core.
            // Apparently listener.GetContext() doesn't throw an exception if listener.Stop() is called,
            // like it does in .NET Framework.
            Environment.Exit(0);
        }
예제 #2
0
#pragma warning disable 1998
        public static async Task Main(string[] args)
#pragma warning restore 1998
        {
            bool tracingDisabled = args.Any(arg => arg.Equals("TracingDisabled", StringComparison.OrdinalIgnoreCase));

            Console.WriteLine($"TracingDisabled {tracingDisabled}");

            string port = args.FirstOrDefault(arg => arg.StartsWith("Port="))?.Split('=')[1] ?? "9000";

            Console.WriteLine($"Port {port}");

            using (var server = WebServer.Start(port, out var url))
            {
                server.RequestHandler = HandleHttpRequests;

                Console.WriteLine();
                Console.WriteLine($"Starting HTTP listener at {url}");

                // send async http requests using HttpClient
                Console.WriteLine();
                Console.WriteLine("Sending async request with default HttpClient.");
                using (var client = new HttpClient())
                {
                    RequestHelpers.SendAsyncHttpClientRequests(client, tracingDisabled, url, RequestContent);
                }

                // send async http requests using HttpClient with CustomHandler
                Console.WriteLine();
                Console.WriteLine("Sending async request with HttpClient(CustomHandler).");
                using (var client = new HttpClient(new CustomHandler()))
                {
                    RequestHelpers.SendAsyncHttpClientRequests(client, tracingDisabled, url, RequestContent);
                }

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // send async http requests using HttpClient with raw WinHttpHandler
                    Console.WriteLine();
                    Console.WriteLine("Sending async request with HttpClient(WinHttpHandler).");
                    using (var client = new HttpClient(new WinHttpHandler()))
                    {
                        RequestHelpers.SendAsyncHttpClientRequests(client, tracingDisabled, url, RequestContent);
                    }
                }

#if NETCOREAPP
                // send async http requests using HttpClient with raw SocketsHttpHandler
                Console.WriteLine();
                Console.WriteLine("Sending async request with HttpClient(SocketsHttpHandler).");
                using (var client = new HttpClient(new SocketsHttpHandler()))
                {
                    RequestHelpers.SendAsyncHttpClientRequests(client, tracingDisabled, url, RequestContent);
                }
#endif

#if NET5_0_OR_GREATER
                // send sync http requests using HttpClient
                Console.WriteLine();
                Console.WriteLine("Sending sync request with default HttpClient.");
                using (var client = new HttpClient())
                {
                    RequestHelpers.SendHttpClientRequests(client, tracingDisabled, url, RequestContent);
                }

                // send async http requests using HttpClient with CustomHandler
                Console.WriteLine();
                Console.WriteLine("Sending sync request with HttpClient(CustomHandler).");
                using (var client = new HttpClient(new CustomHandler()))
                {
                    RequestHelpers.SendHttpClientRequests(client, tracingDisabled, url, RequestContent);
                }

                // send sync http requests using HttpClient with raw SocketsHttpHandler
                Console.WriteLine();
                Console.WriteLine("Sending sync request with HttpClient(SocketsHttpHandler).");
                using (var client = new HttpClient(new SocketsHttpHandler()))
                {
                    RequestHelpers.SendHttpClientRequests(client, tracingDisabled, url, RequestContent);
                }

                // sync http requests using HttpClient are not supported with WinHttpHandler
#endif

#if NETCOREAPP2_1 || NETCOREAPP3_0 || NETCOREAPP3_1
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Console.WriteLine();
                    Console.WriteLine("Sending async request with internal WinHttpHandler.");
                    Type winHttpHandler = typeof(System.Net.Http.HttpMessageHandler).Assembly.GetTypes().FirstOrDefault(t => t.Name == "WinHttpHandler");
                    System.Net.Http.HttpMessageHandler handler = (System.Net.Http.HttpMessageHandler)Activator.CreateInstance(winHttpHandler);
                    using (var invoker = new HttpMessageInvoker(handler, false))
                    {
                        await RequestHelpers.SendHttpMessageInvokerRequestsAsync(invoker, tracingDisabled, url);
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Sending async request with CurlHandler.");
                    Type curlHandlerType = typeof(System.Net.Http.HttpMessageHandler).Assembly.GetTypes().FirstOrDefault(t => t.Name == "CurlHandler");
                    System.Net.Http.HttpMessageHandler handler = (System.Net.Http.HttpMessageHandler)Activator.CreateInstance(curlHandlerType);
                    using (var invoker = new HttpMessageInvoker(handler, false))
                    {
                        await RequestHelpers.SendHttpMessageInvokerRequestsAsync(invoker, tracingDisabled, url);
                    }
                }
#endif

                Console.WriteLine();
                Console.WriteLine("Stopping HTTP listener.");
            }

            // Force process to end, otherwise the background listener thread lives forever in .NET Core.
            // Apparently listener.GetContext() doesn't throw an exception if listener.Stop() is called,
            // like it does in .NET Framework.
            Environment.Exit(0);
        }
        public static async Task Main(string[] args)
        {
            bool tracingDisabled = args.Any(arg => arg.Equals("TracingDisabled", StringComparison.OrdinalIgnoreCase));

            Console.WriteLine($"TracingDisabled {tracingDisabled}");

            string port = args.FirstOrDefault(arg => arg.StartsWith("Port="))?.Split('=')[1] ?? "9000";

            Console.WriteLine($"Port {port}");

            using (var listener = StartHttpListenerWithPortResilience(port))
            {
                Console.WriteLine();
                Console.WriteLine($"Starting HTTP listener at {Url}");

                // send async http requests using HttpClient
                Console.WriteLine();
                Console.WriteLine("Sending async request with default HttpClient.");
                using (var client = new HttpClient())
                {
                    await RequestHelpers.SendHttpClientRequestsAsync(client, tracingDisabled, Url, RequestContent);
                }

                // send async http requests using HttpClient with CustomHandler
                Console.WriteLine();
                Console.WriteLine("Sending async request with HttpClient(CustomHandler).");
                using (var client = new HttpClient(new CustomHandler()))
                {
                    await RequestHelpers.SendHttpClientRequestsAsync(client, tracingDisabled, Url, RequestContent);
                }

#if !NET452
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // send async http requests using HttpClient with raw WinHttpHandler
                    Console.WriteLine();
                    Console.WriteLine("Sending async request with HttpClient(WinHttpHandler).");
                    using (var client = new HttpClient(new WinHttpHandler()))
                    {
                        await RequestHelpers.SendHttpClientRequestsAsync(client, tracingDisabled, Url, RequestContent);
                    }
                }
#endif

#if NETCOREAPP
                // send async http requests using HttpClient with raw SocketsHttpHandler
                Console.WriteLine();
                Console.WriteLine("Sending async request with HttpClient(SocketsHttpHandler).");
                using (var client = new HttpClient(new SocketsHttpHandler()))
                {
                    await RequestHelpers.SendHttpClientRequestsAsync(client, tracingDisabled, Url, RequestContent);
                }
#endif

#if NET5_0
                // send sync http requests using HttpClient
                Console.WriteLine();
                Console.WriteLine("Sending sync request with default HttpClient.");
                using (var client = new HttpClient())
                {
                    RequestHelpers.SendHttpClientRequests(client, tracingDisabled, Url, RequestContent);
                }

                // send async http requests using HttpClient with CustomHandler
                Console.WriteLine();
                Console.WriteLine("Sending sync request with HttpClient(CustomHandler).");
                using (var client = new HttpClient(new CustomHandler()))
                {
                    RequestHelpers.SendHttpClientRequests(client, tracingDisabled, Url, RequestContent);
                }

                // send sync http requests using HttpClient with raw SocketsHttpHandler
                Console.WriteLine();
                Console.WriteLine("Sending sync request with HttpClient(SocketsHttpHandler).");
                using (var client = new HttpClient(new SocketsHttpHandler()))
                {
                    RequestHelpers.SendHttpClientRequests(client, tracingDisabled, Url, RequestContent);
                }

                // sync http requests using HttpClient are not supported with WinHttpHandler
#endif
                Console.WriteLine();
                Console.WriteLine("Stopping HTTP listener.");
                listener.Stop();
            }

            // Force process to end, otherwise the background listener thread lives forever in .NET Core.
            // Apparently listener.GetContext() doesn't throw an exception if listener.Stop() is called,
            // like it does in .NET Framework.
            Environment.Exit(0);
        }