HttpClient

HttpSelfHostServer server = null;
            try
            {
                // Set up server configuration
                HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(_baseAddress);
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

                // Create server
                server = new HttpSelfHostServer(config);

                // Start listening
                server.OpenAsync().Wait();
                Console.WriteLine("Listening on " + _baseAddress);

                // Call the web API and display the result
                HttpClient client = new HttpClient();
                client.GetStringAsync(_address).ContinueWith(
                    getTask =>
                    {
                        if (getTask.IsCanceled)
                        {
                            Console.WriteLine("Request was canceled");
                        }
                        else if (getTask.IsFaulted)
                        {
                            Console.WriteLine("Request failed: {0}", getTask.Exception);
                        }
                        else
                        {
                            Console.WriteLine("Client received: {0}", getTask.Result);
                        }
                    });
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Could not start server: {0}", e.GetBaseException().Message);
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
            finally
            {
                if (server != null)
                {
                    // Stop listening
                    server.CloseAsync().Wait();
                }
            }

 

posted on 2017-03-26 22:24  jafic  阅读(171)  评论(0编辑  收藏  举报