封装后带超时的httpclient

nuget包里的。点我查看安装命令

参考自菜菜的这篇文章

 public MainPage()
        {
            InitializeComponent();
            method();//注意:如果2个method写在一个方法里面,调用的时候,2个方法走完才会加载数据,这样用户体验很不好
            method1();
        }

        private  async void method()
        {
            var a = await AsyncCallbac1("http://www.baidu.com");
            TbBlock.Text = a;
        }
          private  async void method1()
        {
             var b = await AsyncCallbac1("http://www.google.com");
            TtBlock.Text = b;
          }
        private async Task<string> AsyncCallbac1(string url)
        {
            try
            {
                var hc = new HttpClient();
                var hrm = new HttpRequestMessage(HttpMethod.Post, url);
                hc.Timeout = TimeSpan.FromSeconds(3);
                string content = await (await hc.SendAsync(hrm)).Content.ReadAsStringAsync();
                return content;
            }
            catch (Exception ex)
            {
                MessageBox.Show("hello");
                return null;
            }
        }

  

posted on 2014-07-24 22:21  鸣动我心  阅读(256)  评论(0编辑  收藏  举报