怎么用wordpress 建站,萝卜建站下载,如何完整保存网站并做修改,查公司法人信息怎么查WebClient运行于UI线程#xff0c;支持编码方式的设定、支持POST/GET方式提交、不支持同步请求、不支持超时设定。WP7会缓存URL链 接#xff0c;所以两次请求#xff0c;尽管网络端数据发生了变化#xff0c;得到的还会是同样的数据#xff0c;这点要特别注意#xff0c;… WebClient运行于UI线程支持编码方式的设定、支持POST/GET方式提交、不支持同步请求、不支持超时设定。WP7会缓存URL链 接所以两次请求尽管网络端数据发生了变化得到的还会是同样的数据这点要特别注意避免的方式是在URL的末端加一个当前时间的参数这样每次请 求的url都不一样从而避免的缓存的影响。另外要说的是WebClient不适合大数据量的的请求那样会造成UI线程的繁忙最终导致无法响应用户 的操作。当然WebClient也有它的优点因为经过了封装用起来方面也无需做太多的设置适合小数据量的请求。 实例1用post方式提交数据 Uri url new Uri(“http//:www.163.com”);string str namename1mo 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.Errornull){// 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 datao.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下载数据这个暂时还没用到先预留位置在此 http://www.cnblogs.com/dyg540/articles/2514773.html转载于:https://www.cnblogs.com/zziss/archive/2012/10/23/2736018.html