跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
C Sharp WebClient 类
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= C# WebClient类 = == 介绍 == '''WebClient''' 是 .NET Framework 提供的一个高级别网络编程类,用于简化常见的网络操作,如下载和上传数据。它封装了底层的 <code>HttpWebRequest</code> 和 <code>HttpWebResponse</code> 类,使得开发者能够以更少的代码完成网络请求。 WebClient 类适用于以下场景: * 下载文件或数据(如文本、JSON、XML) * 上传数据到服务器 * 处理 FTP 请求 * 异步操作 == 基本用法 == WebClient 提供了一系列方法用于执行网络操作,包括同步和异步方式。 === 下载数据 === 以下示例展示如何使用 WebClient 下载一个网页的内容: <syntaxhighlight lang="csharp"> using System; using System.Net; class Program { static void Main() { using (WebClient client = new WebClient()) { string url = "https://example.com"; string content = client.DownloadString(url); Console.WriteLine(content); } } } </syntaxhighlight> '''输出:''' <pre> <!doctype html> <html> <head> <title>Example Domain</title> ... </html> </pre> === 下载文件 === WebClient 也可以用于下载文件到本地: <syntaxhighlight lang="csharp"> using System; using System.Net; class Program { static void Main() { using (WebClient client = new WebClient()) { string fileUrl = "https://example.com/image.jpg"; string localPath = "C:\\Downloads\\image.jpg"; client.DownloadFile(fileUrl, localPath); Console.WriteLine("文件下载完成!"); } } } </syntaxhighlight> == 异步操作 == WebClient 提供了异步方法,以避免阻塞主线程。 === 异步下载示例 === <syntaxhighlight lang="csharp"> using System; using System.Net; using System.Threading.Tasks; class Program { static async Task Main() { using (WebClient client = new WebClient()) { string url = "https://example.com"; string content = await client.DownloadStringTaskAsync(url); Console.WriteLine(content); } } } </syntaxhighlight> == 上传数据 == WebClient 支持向服务器上传数据,如表单数据或文件。 === 上传字符串数据 === <syntaxhighlight lang="csharp"> using System; using System.Net; using System.Collections.Specialized; class Program { static void Main() { using (WebClient client = new WebClient()) { string url = "https://example.com/api/submit"; NameValueCollection data = new NameValueCollection(); data["username"] = "user1"; data["password"] = "pass123"; byte[] response = client.UploadValues(url, "POST", data); string responseText = System.Text.Encoding.UTF8.GetString(response); Console.WriteLine(responseText); } } } </syntaxhighlight> == 事件处理 == WebClient 提供事件来监控下载进度或完成状态。 === 进度事件示例 === <syntaxhighlight lang="csharp"> using System; using System.Net; class Program { static void Main() { using (WebClient client = new WebClient()) { client.DownloadProgressChanged += (sender, e) => { Console.WriteLine($"下载进度: {e.ProgressPercentage}%"); }; client.DownloadFileCompleted += (sender, e) => { Console.WriteLine("下载完成!"); }; string fileUrl = "https://example.com/largefile.zip"; string localPath = "C:\\Downloads\\largefile.zip"; client.DownloadFileAsync(new Uri(fileUrl), localPath); Console.ReadLine(); // 等待异步下载完成 } } } </syntaxhighlight> == 实际应用案例 == WebClient 可用于多种实际场景,例如: * 从 API 获取 JSON 数据 * 下载远程文件(如图片、文档) * 提交表单数据到服务器 * 自动化数据抓取 === 案例:获取天气数据 === <syntaxhighlight lang="csharp"> using System; using System.Net; using Newtonsoft.Json.Linq; class Program { static void Main() { using (WebClient client = new WebClient()) { string apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY"; string json = client.DownloadString(apiUrl); JObject weatherData = JObject.Parse(json); Console.WriteLine($"城市: {weatherData["name"]}"); Console.WriteLine($"温度: {weatherData["main"]["temp"]}K"); } } } </syntaxhighlight> == 注意事项 == * WebClient 在 .NET Core/.NET 5+ 中仍可用,但推荐使用更现代的 <code>HttpClient</code>。 * 对于复杂请求(如自定义 Headers、Cookie 管理),建议使用 <code>HttpClient</code>。 * 确保正确处理异常(如网络错误、无效 URL)。 == 总结 == WebClient 是一个简单易用的类,适合快速实现网络操作。它提供了同步和异步方法,支持下载、上传和事件监控。对于初学者来说,WebClient 是学习网络编程的良好起点,但在生产环境中,可能需要考虑更灵活的替代方案(如 <code>HttpClient</code>)。 == 参见 == * [[C# HttpClient 类]] * [[C# 网络编程基础]] * [[C# 异步编程]] [[Category:编程语言]] [[Category:C Sharp]] [[Category:C Sharp 网络编程]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)