WebClient.UploadValues 方法

定義

將名稱/值集合上載至具有指定之 URI 的資源。Uploads a name/value collection to a resource with the specified URI.

多載

UploadValues(String, NameValueCollection)

將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified URI.

UploadValues(Uri, NameValueCollection)

將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified URI.

UploadValues(String, String, NameValueCollection)

使用指定的方法,將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified URI, using the specified method.

UploadValues(Uri, String, NameValueCollection)

使用指定的方法,將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified URI, using the specified method.

UploadValues(String, NameValueCollection)

將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified 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。The URI of the resource to receive the collection.

data
NameValueCollection

要傳送至資源的 NameValueCollectionThe NameValueCollection to send to the resource.

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。A Byte array containing the body of the response from the resource.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress, and address is invalid.

-或--or- datanulldata is null.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- Content-type 標頭不是 null 或 "application/x-www-form-urlencoded"。The Content-type header is not null or "application/x-www-form-urlencoded".

範例

下列程式碼範例會向使用者收集資訊(名稱、年齡和位址),並使用 UploadValues將值張貼至伺服器。The following code example gathers information from the user (name, age, and address) and posts the values to the server using UploadValues. 伺服器的任何回應都會顯示在主控台上。Any response from the server is displayed on the console.

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 傳送至伺服器。The UploadValues method sends a NameValueCollection to a server. 這個方法會在上傳資料時封鎖。This method blocks while uploading the data. 若要在等候伺服器回應時繼續執行,請使用其中一種 UploadValuesAsync 方法。To continue executing while waiting for the server's response, use one of the UploadValuesAsync methods.

如果伺服器無法辨識基礎要求,則基礎通訊協定類別會決定發生什麼事。If the underlying request is not understood by the server, the underlying protocol classes determine what occurs. 通常會擲回 WebException,並設定 Status 屬性來指出錯誤。Typically, a WebException is thrown with the Status property set to indicate the error.

如果 Content-type 標頭 nullUploadValues 方法會將它設定為 "application/x-www-urlencoded"。If the Content-type header is null, the UploadValues method sets it to "application/x-www-form-urlencoded".

如果 BaseAddress 屬性不是空字串(""),而且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

UploadValues(Uri, NameValueCollection)

將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified 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。The URI of the resource to receive the collection.

data
NameValueCollection

要傳送至資源的 NameValueCollectionThe NameValueCollection to send to the resource.

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。A Byte array containing the body of the response from the resource.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress, and address is invalid.

-或--or- datanulldata is null.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- Content-type 標頭不是 null 或 "application/x-www-form-urlencoded"。The Content-type header is not null or "application/x-www-form-urlencoded".

備註

UploadValues 方法會將 NameValueCollection 傳送至伺服器。The UploadValues method sends a NameValueCollection to a server. 這個方法會在上傳資料時封鎖。This method blocks while uploading the data. 若要在等候伺服器回應時繼續執行,請使用其中一種 UploadValuesAsync 方法。To continue executing while waiting for the server's response, use one of the UploadValuesAsync methods.

如果伺服器無法辨識基礎要求,則基礎通訊協定類別會決定發生什麼事。If the underlying request is not understood by the server, the underlying protocol classes determine what occurs. 通常會擲回 WebException,並設定 Status 屬性來指出錯誤。Typically, a WebException is thrown with the Status property set to indicate the error.

如果 Content-type 標頭 nullUploadValues 方法會將它設定為 "application/x-www-urlencoded"。If the Content-type header is null, the UploadValues method sets it to "application/x-www-form-urlencoded".

如果 BaseAddress 屬性不是空字串(""),而且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

UploadValues(String, String, NameValueCollection)

使用指定的方法,將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified URI, using the specified method.

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);
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。The URI of the resource to receive the collection.

method
String

傳送檔案給資源所使用的 HTTP 方法。The HTTP method used to send the file to the resource. 如果是 Null,預設的 HTTP 為 POST,而 FTP 則為 STOR。If null, the default is POST for http and STOR for ftp.

data
NameValueCollection

要傳送至資源的 NameValueCollectionThe NameValueCollection to send to the resource.

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。A Byte array containing the body of the response from the resource.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress, and address is invalid.

-或--or- datanulldata is null.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- Content-type 標頭值不是 null,也不是 application/x-www-form-urlencodedThe Content-type header value is not null and is not application/x-www-form-urlencoded.

範例

下列程式碼範例會向使用者收集資訊(名稱、年齡和位址),並使用 UploadValues將值張貼至伺服器。The following code example gathers information from the user (name, age, and address) and posts the values to the server using UploadValues. 伺服器的任何回應都會顯示在主控台上。Any response from the server is displayed on the console.

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 方法會使用 method 參數中指定的方法,將 NameValueCollection 傳送至資源,並從伺服器傳回任何回應。The UploadValues method sends a NameValueCollection to a resource using the method specified in the method parameter and returns any response from the server. 這個方法會在上傳資料時封鎖。This method blocks while uploading the data. 若要在等候伺服器回應時繼續執行,請使用其中一種 UploadValuesAsync 方法。To continue executing while waiting for the server's response, use one of the UploadValuesAsync methods.

如果 Content-type 標頭 nullUploadValues 方法會將它設定為 application/x-www-form-urlencodedIf the Content-type header is null, the UploadValues method sets it to application/x-www-form-urlencoded.

如果 method 參數指定了伺服器不了解的動詞,則基礎通訊協定類別會決定發生的動作。If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. 通常會擲回 WebException,並設定 Status 屬性來指出錯誤。Typically, a WebException is thrown with the Status property set to indicate the error.

如果 BaseAddress 屬性不是空字串(""),而且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

UploadValues(Uri, String, NameValueCollection)

使用指定的方法,將指定的名稱/值集合上載至由指定之 URI 所識別的資源。Uploads the specified name/value collection to the resource identified by the specified URI, using the specified method.

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);
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。The URI of the resource to receive the collection.

method
String

傳送檔案給資源所使用的 HTTP 方法。The HTTP method used to send the file to the resource. 如果是 Null,預設的 HTTP 為 POST,而 FTP 則為 STOR。If null, the default is POST for http and STOR for ftp.

data
NameValueCollection

要傳送至資源的 NameValueCollectionThe NameValueCollection to send to the resource.

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。A Byte array containing the body of the response from the resource.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

合併 BaseAddressaddress 而形成的 URI 是無效的。The URI formed by combining BaseAddress, and address is invalid.

-或--or- datanulldata is null.

-或--or- 開啟資料流時發生錯誤。An error occurred while opening the stream.

-或--or- 沒有來自裝載 (Host) 資源之伺服器的回應。There was no response from the server hosting the resource.

-或--or- Content-type 標頭值不是 null,也不是 application/x-www-form-urlencodedThe Content-type header value is not null and is not application/x-www-form-urlencoded.

備註

UploadValues 方法會使用 method 參數中指定的方法,將 NameValueCollection 傳送至資源,並從伺服器傳回任何回應。The UploadValues method sends a NameValueCollection to a resource using the method specified in the method parameter and returns any response from the server. 這個方法會在上傳資料時封鎖。This method blocks while uploading the data. 若要在等候伺服器回應時繼續執行,請使用其中一種 UploadValuesAsync 方法。To continue executing while waiting for the server's response, use one of the UploadValuesAsync methods.

如果 Content-type 標頭 nullUploadValues 方法會將它設定為 application/x-www-form-urlencodedIf the Content-type header is null, the UploadValues method sets it to application/x-www-form-urlencoded.

如果 method 參數指定了伺服器不了解的動詞,則基礎通訊協定類別會決定發生的動作。If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. 通常會擲回 WebException,並設定 Status 屬性來指出錯誤。Typically, a WebException is thrown with the Status property set to indicate the error.

如果 BaseAddress 屬性不是空字串(""),而且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求資料的絕對 URI。If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. 如果 QueryString 屬性不是空字串,則會將它附加至 addressIf the QueryString property is not an empty string, it is appended to address.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。This member outputs trace information when you enable network tracing in your application. 如需詳細資訊,請參閱.NET Framework 中的網路追蹤For more information, see Network Tracing in the .NET Framework.

適用於