Loading

HttpClient异步调用WEB服务

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始");
            GetData();
            Console.WriteLine("结束");
            Console.ReadKey();
        }

        private static async void GetData()
        {
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = null;
            response = await httpClient.GetAsync("http://wcf.open.cnblogs.com/blog/sitehome/recent/10");
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Response Status Code:" + response.StatusCode + " " + response.ReasonPhrase);
                string responseBodyAsText = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(responseBodyAsText);
            }

        }
    }
}

 

posted @ 2015-08-07 15:29  眾尋  阅读(401)  评论(0编辑  收藏  举报