微软官网方法,绝对经典:
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#进行网络请求的经典方法,通过微软官网推荐的代码示例,展示了如何从指定URL下载资源并将其转换为字节数组。此方法适用于需要在网络环境中获取数据的应用场景。
7415

被折叠的 条评论
为什么被折叠?



