WebClient.UploadValues 方法

定义

将名称/值集合上载到具有指定 URI 的资源。

重载

UploadValues(String, NameValueCollection)

将指定的名称/值集合上载到指定的 URI 所标识的资源。

UploadValues(Uri, NameValueCollection)

将指定的名称/值集合上载到指定的 URI 所标识的资源。

UploadValues(String, String, NameValueCollection)

使用指定的方法将指定的名称/值集合上载到指定的 URI 所标识的资源。

UploadValues(Uri, String, NameValueCollection)

使用指定的方法将指定的名称/值集合上载到指定的 URI 所标识的资源。

UploadValues(String, NameValueCollection)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将指定的名称/值集合上载到指定的 URI 所标识的资源。

public:
 cli::array <System::Byte> ^ UploadValues(System::String ^ address, System::Collections::Specialized::NameValueCollection ^ data);
public byte[] UploadValues (string address, System.Collections.Specialized.NameValueCollection data);
member this.UploadValues : string * System.Collections.Specialized.NameValueCollection -> byte[]
Public Function UploadValues (address As String, data As NameValueCollection) As Byte()

参数

address
String

接收集合的资源的 URI。

data
NameValueCollection

要发送到资源的 NameValueCollection

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

data 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

承载资源的服务器没有响应。

- 或 -

打开流时发生错误。

- 或 -

Content-type 标头不是 null 或“application/x-www-form-urlencoded”。

示例

以下代码示例从用户 (名称、年龄和地址) 收集信息,并使用 UploadValues将值发布到服务器。 来自服务器的任何响应都显示在控制台上。

Console::Write( "\nPlease enter the URI to post data to: " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;

// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection^ myNameValueCollection = gcnew NameValueCollection;

Console::WriteLine( "Please enter the following parameters to be posted to the URL" );
Console::Write( "Name: " );
String^ name = Console::ReadLine();

Console::Write( "Age: " );
String^ age = Console::ReadLine();

Console::Write( "Address: " );
String^ address = Console::ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection->Add( "Name", name );
myNameValueCollection->Add( "Address", address );
myNameValueCollection->Add( "Age", age );

Console::WriteLine( "\nUploading to {0} ...", uriString );
// 'The Upload(String, NameValueCollection)' implicitly method sets HTTP POST as the request method.
array<Byte>^ responseArray = myWebClient->UploadValues( uriString, myNameValueCollection );

// Decode and display the response.
Console::WriteLine( "\nResponse received was :\n {0}", Encoding::ASCII->GetString( responseArray ) );
Console.Write("\nPlease enter the URI to post data to : ");
string uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection myNameValueCollection = new NameValueCollection();

Console.WriteLine("Please enter the following parameters to be posted to the URL");
Console.Write("Name:");
string name = Console.ReadLine();

Console.Write("Age:");
string age = Console.ReadLine();

Console.Write("Address:");
string address = Console.ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name",name);            
myNameValueCollection.Add("Address",address);
myNameValueCollection.Add("Age",age);

Console.WriteLine("\nUploading to {0} ...",  uriString);
// 'The Upload(String,NameValueCollection)' implicitly method sets HTTP POST as the request method.            
byte[] responseArray = myWebClient.UploadValues(uriString,myNameValueCollection);

// Decode and display the response.
Console.WriteLine("\nResponse received was :\n{0}",Encoding.ASCII.GetString(responseArray));
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
Dim myNameValueCollection As New NameValueCollection()
Console.WriteLine("Please enter the following parameters to be posted to the URL:")
Console.Write("Name:")
Dim name As String = Console.ReadLine()

Console.Write("Age:")
Dim age As String = Console.ReadLine()

Console.Write("Address:")
Dim address As String = Console.ReadLine()

' Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name", name)
myNameValueCollection.Add("Address", address)
myNameValueCollection.Add("Age", age)

Console.WriteLine(ControlChars.Cr + "Uploading to {0} ...", uriString)
' The Upload(String,NameValueCollection)' method implicitly sets the HTTP POST as the request method.			
Dim responseArray As Byte() = myWebClient.UploadValues(uriString, myNameValueCollection)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :" + ControlChars.Cr + "{0}", Encoding.ASCII.GetString(responseArray))

注解

方法 UploadValues 将 发送到 NameValueCollection 服务器。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadValuesAsync 一。

如果服务器无法理解基础请求,基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

如果 Content-type 标头为 null,该方法 UploadValues 将其设置为“application/x-www-form-urlencoded”。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadValues(Uri, NameValueCollection)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

将指定的名称/值集合上载到指定的 URI 所标识的资源。

public:
 cli::array <System::Byte> ^ UploadValues(Uri ^ address, System::Collections::Specialized::NameValueCollection ^ data);
public byte[] UploadValues (Uri address, System.Collections.Specialized.NameValueCollection data);
member this.UploadValues : Uri * System.Collections.Specialized.NameValueCollection -> byte[]
Public Function UploadValues (address As Uri, data As NameValueCollection) As Byte()

参数

address
Uri

接收集合的资源的 URI。

data
NameValueCollection

要发送到资源的 NameValueCollection

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

data 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

承载资源的服务器没有响应。

- 或 -

打开流时发生错误。

- 或 -

Content-type 标头不是 null 或“application/x-www-form-urlencoded”。

注解

方法 UploadValues 将 发送到 NameValueCollection 服务器。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadValuesAsync 一。

如果服务器无法理解基础请求,基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

如果 Content-type 标头为 null,该方法 UploadValues 将其设置为“application/x-www-form-urlencoded”。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadValues(String, String, NameValueCollection)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

使用指定的方法将指定的名称/值集合上载到指定的 URI 所标识的资源。

public:
 cli::array <System::Byte> ^ UploadValues(System::String ^ address, System::String ^ method, System::Collections::Specialized::NameValueCollection ^ data);
public byte[] UploadValues (string address, string? method, System.Collections.Specialized.NameValueCollection data);
public byte[] UploadValues (string address, string method, System.Collections.Specialized.NameValueCollection data);
member this.UploadValues : string * string * System.Collections.Specialized.NameValueCollection -> byte[]
Public Function UploadValues (address As String, method As String, data As NameValueCollection) As Byte()

参数

address
String

接收集合的资源的 URI。

method
String

用于将文件发送到资源的 HTTP 方法。 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。

data
NameValueCollection

要发送到资源的 NameValueCollection

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

data 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

打开流时发生错误。

- 或 -

承载资源的服务器没有响应。

- 或 -

Content-type 标头值既不是 null 也不是 application/x-www-form-urlencoded

示例

以下代码示例从用户 (名称、年龄和地址) 收集信息,并使用 UploadValues将值发布到服务器。 来自服务器的任何响应都显示在控制台上。

Console::Write( "\nPlease enter the URL to post data to: " );
String^ uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;

// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection^ myNameValueCollection = gcnew NameValueCollection;

Console::WriteLine( "Please enter the following parameters to be posted to the URI" );
Console::Write( "Name: " );
String^ name = Console::ReadLine();

Console::Write( "Age: " );
String^ age = Console::ReadLine();

Console::Write( "Address: " );
String^ address = Console::ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection->Add( "Name", name );
myNameValueCollection->Add( "Address", address );
myNameValueCollection->Add( "Age", age );
Console::WriteLine( "\nUploading to {0} ...", uriString );

// Upload the NameValueCollection.
array<Byte>^ responseArray = myWebClient->UploadValues( uriString, "POST", myNameValueCollection );

// Decode and display the response.
Console::WriteLine( "\nResponse received was :\n {0}", Encoding::ASCII->GetString( responseArray ) );
Console.Write("\nPlease enter the URL to post data to : ");
string uriString = Console.ReadLine();

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
NameValueCollection myNameValueCollection = new NameValueCollection();

Console.WriteLine("Please enter the following parameters to be posted to the URI");
Console.Write("Name:");
string name = Console.ReadLine();

Console.Write("Age:");
string age = Console.ReadLine();

Console.Write("Address:");
string address = Console.ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name",name);			
myNameValueCollection.Add("Address",address);
myNameValueCollection.Add("Age",age);
Console.WriteLine("\nUploading to {0} ...",  uriString);

// Upload the NameValueCollection.
byte[] responseArray = myWebClient.UploadValues(uriString,"POST",myNameValueCollection);

// Decode and display the response.
Console.WriteLine("\nResponse received was :\n{0}",Encoding.ASCII.GetString(responseArray));
Console.Write(ControlChars.Cr + "Please enter the URL to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()

' Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
Dim myNameValueCollection As New NameValueCollection()

Console.WriteLine("Please enter the following parameters to be posted to the Url")
Console.Write("Name:")
Dim name As String = Console.ReadLine()

Console.Write("Age:")
Dim age As String = Console.ReadLine()

Console.Write("Address:")
Dim address As String = Console.ReadLine()

' Add necessary parameter/value pairs to the name/value container.
myNameValueCollection.Add("Name", name)
myNameValueCollection.Add("Address", address)
myNameValueCollection.Add("Age", age)

Console.WriteLine(ControlChars.Cr + "Uploading to {0} ...", uriString)

' Upload the NameValueCollection.
Dim responseArray As Byte() = myWebClient.UploadValues(uriString, "POST", myNameValueCollection)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :" + ControlChars.Cr + "{0}", Encoding.ASCII.GetString(responseArray))

注解

方法 UploadValues 使用 参数中指定的方法将 发送到 NameValueCollection 资源, method 并返回服务器的任何响应。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadValuesAsync 一。

如果 标头为 Content-typenull,则 UploadValues 方法将其设置为 application/x-www-form-urlencoded

method如果 参数指定了服务器无法理解的谓词,则基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

UploadValues(Uri, String, NameValueCollection)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
WebClient.cs

使用指定的方法将指定的名称/值集合上载到指定的 URI 所标识的资源。

public:
 cli::array <System::Byte> ^ UploadValues(Uri ^ address, System::String ^ method, System::Collections::Specialized::NameValueCollection ^ data);
public byte[] UploadValues (Uri address, string? method, System.Collections.Specialized.NameValueCollection data);
public byte[] UploadValues (Uri address, string method, System.Collections.Specialized.NameValueCollection data);
member this.UploadValues : Uri * string * System.Collections.Specialized.NameValueCollection -> byte[]
Public Function UploadValues (address As Uri, method As String, data As NameValueCollection) As Byte()

参数

address
Uri

接收集合的资源的 URI。

method
String

用于将文件发送到资源的 HTTP 方法。 如果为 null,则对于 http 默认值为 POST,对于 ftp 默认值为 STOR。

data
NameValueCollection

要发送到资源的 NameValueCollection

返回

Byte[]

一个 Byte 数组,它包含来自资源的响应的正文。

例外

address 参数为 null

- 或 -

data 参数为 null

通过组合 BaseAddressaddress 所构成的 URI 无效。

- 或 -

datanull

- 或 -

打开流时发生错误。

- 或 -

承载资源的服务器没有响应。

- 或 -

Content-type 标头值既不是 null 也不是 application/x-www-form-urlencoded

注解

方法 UploadValues 使用 参数中指定的方法将 发送到 NameValueCollection 资源, method 并返回服务器的任何响应。 此方法在上传数据时阻止。 若要在等待服务器的响应时继续执行,请使用 方法之 UploadValuesAsync 一。

如果 标头为 Content-typenull,则 UploadValues 方法将其设置为 application/x-www-form-urlencoded

method如果 参数指定了服务器无法理解的谓词,则基础协议类将确定所发生的情况。 通常, WebException 会引发 ,并将 Status 属性设置为指示错误。

BaseAddress如果属性不是空字符串 (“”) ,并且address不包含绝对 URI,address则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于