Lost !

-----hard working for the furture.

导航

WP7 网络请求之WebClient

WebClient运行于UI线程,支持编码方式的设定、支持POST/GET方式提交、不支持同步请求、不支持超时设定。WP7会缓存URL链 接,所以两次请求,尽管网络端数据发生了变化,得到的还会是同样的数据,这点要特别注意,避免的方式是在URL的末端,加一个当前时间的参数,这样每次请 求的url都不一样,从而避免的缓存的影响。另外要说的是,WebClient不适合大数据量的的请求,那样会造成UI线程的繁忙,最终导致无法响应用户 的操作。当然WebClient也有它的优点,因为经过了封装,用起来方面,也无需做太多的设置,适合小数据量的请求。

实例1:用post方式提交数据

 Uri url = new Uri(“http//:www.163.com”);
 string str = "name=name1&mo=" + HttpUtility.UrlEncode("中文数据和特殊字符最好编码一下") + "&Cache=" + System.DateTime.Now;  

 WebClient webClient = new WebClient();
 webClient.Encoding = System.Text.UTF8Encoding.UTF8;
  webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

  //webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(webClient_UploadProgressChanged);//这个是
  webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(CardInfoUp_Completed);//这里是回调函数
  webClient.UploadStringAsync(url, "POST", str);

private void CardInfoUp_Completed(object sender, UploadStringCompletedEventArgs e)
 {
        if(e.Error==null)
        {
               // XElement strXml = XElement.Parse(e.Result);//这是网络返回的数据
                MessageBox.Show("成功!");
       }
       else
       {
               MessageBox.Show(e.Error.Message);
       }                   
  }

如果想代码更简洁一些,可以使用匿名函数,像下面这样:

           WebClient webClient = new WebClient();
            webClient.Encoding = System.Text.UTF8Encoding.UTF8;           
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";        
            webClient.UploadStringCompleted += (s, o) =>
            {
                if (o.Error == null)
                {
                    //string data=o.Result ;//这是网络返回的数据 
                    MessageBox.Show("成功");
                }
                else
                {
                    throw new Exception(o.Error.Message);
                }
            };
            webClient.UploadStringAsync(url, "POST", str);

实例2:打开网页,可以带参数,如果服务器返回的内容经过加工,可以使用这种方式变相下载数据

        WebClient webClient = new WebClient();
        webClient.OpenReadAsync(url);              //在不阻止调用线程的情况下,从资源返回数据
        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(CardInfoDown_Completed);      //异步操作完成时发生

       private void CardInfoDown_Completed(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {           
                using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Result))
                {
                    string strStream = reader.ReadToEnd();//这里是返回的数据
                    MessageBox.Show("下载成功");                 
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }           
        }

下面是简洁的写法:

            WebClient webClient = new WebClient();
            webClient.Encoding = System.Text.UTF8Encoding.UTF8;
            webClient.OpenReadAsync(url);              //在不阻止调用线程的情况下,从资源返回数据
            webClient.OpenReadCompleted += (s,o) =>
            {
                if (o.Error == null)
                {
                    //指定以UTF-8方式读取流
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(o.Result, System.Text.UTF8Encoding.UTF8))
                    {
                        string strStream = reader.ReadToEnd();//这里是返回的数据
                        MessageBox.Show("下载成功");
                    }
                }
                else
                {
                    MessageBox.Show(o.Error.Message);
                } 
            };

实例2:下载数据,这个暂时还没用到,先预留位置在此

posted on 2012-05-23 14:44  失落''80  阅读(1163)  评论(0编辑  收藏  举报