我是靠谱客的博主 暴躁咖啡,最近开发中收集的这篇文章主要介绍c# 访问网页简单 sample,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

记下来备用。

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所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部