概述
记下来备用。
HttpWebRequest 同步调用
UriBuilder uri = new UriBuilder("http://www.cnblogs.com/xiaokang088/rss.aspx"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); var str = reader.ReadToEnd();
HttpWebRequest 异步调用
public MainWindow() { InitializeComponent(); Loaded += new RoutedEventHandler(MainWindow_Loaded); } void MainWindow_Loaded(object sender, RoutedEventArgs e) { UriBuilder uri = new UriBuilder("http://www.cnblogs.com/xiaokang088/rss.aspx"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri); request.BeginGetResponse(new AsyncCallback(handleResponce), request); } private void handleResponce(IAsyncResult ar) { HttpWebRequest request = ar.AsyncState as HttpWebRequest; var reponse = request.EndGetResponse(ar) as HttpWebResponse; var stream = reponse.GetResponseStream(); StreamReader reader = new StreamReader(stream); string str = reader.ReadToEnd(); }
WebClient ,最简单,注意编码,否则会乱码
WebClient client = new WebClient(); //client.Encoding = Encoding.UTF8; var address = "http://www.cnblogs.com/xiaokang088/rss.aspx"; string content = client.DownloadString(address);
完毕!
最后
以上就是暴躁咖啡为你收集整理的c# 访问网页简单 sample的全部内容,希望文章能够帮你解决c# 访问网页简单 sample所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复