欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > C# >内容正文

C#

C#编程语言之读取网页内容(微软官网方法)

发布时间:2025/5/22 C# 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C#编程语言之读取网页内容(微软官网方法) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

微软官网方法,绝对经典:

public static byte[] GetURLContents(string url){// The downloaded resource ends up in the variable named content.var content = new MemoryStream();// Initialize an HttpWebRequest for the current URL.var webReq = (HttpWebRequest)WebRequest.Create(url);// Send the request to the Internet resource and wait for// the response.// Note: you can't use HttpWebRequest.GetResponse in a Windows Store app.using (WebResponse response = webReq.GetResponse()){// Get the data stream that is associated with the specified URL.using (Stream responseStream = response.GetResponseStream()){// Read the bytes in responseStream and copy them to content.responseStream.CopyTo(content);}}// Return the result as a byte array.return content.ToArray();}

 

总结

以上是生活随笔为你收集整理的C#编程语言之读取网页内容(微软官网方法)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。