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

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

data null

-或-

托管资源的服务器没有响应。

-或-

打开流时出错。

-或-

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))

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

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

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

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

如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 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

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

data null

-或-

托管资源的服务器没有响应。

-或-

打开流时出错。

-或-

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

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

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

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

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

如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 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 和 STOR for ftp。

data
NameValueCollection

要发送到资源的 NameValueCollection

返回

Byte[]

包含来自资源的响应正文的 Byte 数组。

例外

address 参数 null

-或-

data 参数 null

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

data null

-或-

打开流时出错。

-或-

托管资源的服务器没有响应。

-或-

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))

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

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

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

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

如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 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 和 STOR for ftp。

data
NameValueCollection

要发送到资源的 NameValueCollection

返回

Byte[]

包含来自资源的响应正文的 Byte 数组。

例外

address 参数 null

-或-

data 参数 null

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

data null

-或-

打开流时出错。

-或-

托管资源的服务器没有响应。

-或-

Content-type 标头值不 null,并且不 application/x-www-form-urlencoded

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

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

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

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

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

注意

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

适用于