c#测试网络是否联通

///

//From:www.uzhanbao.com
 /// c#测试网络是否联通
        public static bool testIsInternetonline()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://www.cheredu.com");
            // Add an Accept header for JSON format.
            // 为JSON格式添加一个Accept报头
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));


            HttpResponseMessage response = client.GetAsync("api/products").Result;  // Blocking call(阻塞调用)! 
            if (response.IsSuccessStatusCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 

你可能感兴趣的:(C#)