打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
WebClient的详细用法
提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法。不能继承此类。
[Visual Basic]
<ComVisible(True)>
NotInheritable Public Class WebClient
Inherits Component
[C#]
[ComVisible(true)]
public sealed class WebClient : Component
[C++]
[ComVisible(true)]
public __gc __sealed class WebClient : public Component
线程安全
此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。
备注
WebClient 类提供向 URI 标识的任何本地、Intranet 或 Internet 资源发送数据以及从这些资源接收数据的公共方法。
WebClient 类使用 WebRequest 类提供对 Internet 资源的访问。WebClient 实例可以通过任何已向 WebRequest.RegisterPrefix 方法注册的 WebRequest 子代访问数据。
注意 默认情况下,.NET 框架支持以 http:、https: 和 file: 方案标识符开头的 URI。
WebClient 类提供四种将数据上载到资源的方法:
OpenWrite 返回一个用于将数据发送到资源的 Stream。
UploadData 将字节数组发送到资源并返回包含任何响应的字节数组。
UploadFile 将本地文件发送到资源并返回包含任何响应的字节数组。
UploadValues 将 NameValueCollection 发送到资源并返回包含任何响应的字节数组。
WebClient 类还提供三种从资源下载数据的方法:
DownloadData 从资源下载数据并返回字节数组。
DownloadFile 从资源将数据下载到本地文件。
OpenRead 从资源以 Stream 的形式返回数据。
下面的示例创建一个 WebClient 实例,然后使用该实例从服务器下载数据并将其显示在系统控制台上、从服务器下载数据并将其写入文件、以及将窗体值上载到服务器并接收响应。
[Visual Basic]
Public Shared Sub Main()
Try
Dim client As New WebClient()
Dim pageData As [Byte]() = client.DownloadData("http://www.contoso.com")
Dim pageHtml As String = Encoding.ASCII.GetString(pageData)
' Download the data to a buffer.
Console.WriteLine(pageHtml)

' Download the data to a file.
client.DownloadFile("http://www.contoso.com", "page.htm")


' Upload some form post values.
dim form as New NameValueCollection()
form.Add("MyName", "MyValue")
' Note that you need to replace "http://localhost/somefile.aspx"withthenameof
' a file that is available to your computer.
Dim responseData As [Byte]() = client.UploadValues("http://www.contoso.com/form.aspx" , form)
Console.WriteLine(Encoding.ASCII.GetString(responseData))

Catch webEx As WebException
if webEx.Status = WebExceptionStatus.ConnectFailure then
Console.WriteLine("Are you behind a firewall? If so, go through the proxy server.")
end if
Console.Write(webEx.ToString())
End Try

End Sub 'Main
[C#]
try {

// Download the data to a buffer.
WebClient client = new WebClient();
Byte[] pageData = client.DownloadData("http://www.contoso.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);
// Download the data to a file.
client.DownloadFile("http://www.contoso.com", "page.htm");
// Upload some form post values.
NameValueCollection form = new NameValueCollection();
form.Add("MyName", "MyValue");
Byte[] responseData = client.UploadValues("http://www.contoso.com/form.aspx" , form);
}
catch (WebException webEx) {
Console.WriteLine(webEx.ToString());
if(webEx.Status == WebExceptionStatus.ConnectFailure) {
Console.WriteLine("Are you behind a firewall? If so, go through the proxy server.");
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#获取网页内容的三种方式
C#常用集合类使用方法
创建控制台应用程序读取配置文件的内容
.Net中的Collection
C# NameValueCollection集合
C#简单爬虫案例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服