zoukankan      html  css  js  c++  java
  • 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();
                    }
                }
  • 相关阅读:
    优化eclipse
    Servlet与jsp间的传值问题
    servlet & javabean
    Java数据类型
    CentOS 7 安装tomcat
    Nginx配置详解
    PHP文件缓存实现
    lnmp编译安装
    Php安全规范
    php编码规范
  • 原文地址:https://www.cnblogs.com/HalloWorld/p/6624408.html
Copyright © 2011-2022 走看看