共用方式為


WebClient.UploadValues 方法

定義

將名稱/值集合上傳至具有指定URI的資源。

多載

UploadValues(String, NameValueCollection)

將指定的名稱/值集合上傳至指定 URI 所識別的資源。

UploadValues(Uri, NameValueCollection)

將指定的名稱/值集合上傳至指定 URI 所識別的資源。

UploadValues(String, String, NameValueCollection)

使用指定的方法,將指定的名稱/值集合上傳至指定 URI 所識別的資源。

UploadValues(Uri, String, NameValueCollection)

使用指定的方法,將指定的名稱/值集合上傳至指定 URI 所識別的資源。

UploadValues(String, NameValueCollection)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
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))

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

UploadValues 方法會將 NameValueCollection 傳送至伺服器。 這個方法會在上傳數據時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中一個 UploadValuesAsync 方法。

如果伺服器無法了解基礎要求,基礎通訊協定類別會決定會發生什麼情況。 一般而言,會擲回 WebException,並將 Status 屬性設定為表示錯誤。

如果 content-type 標頭是 nullUploadValues 方法會將它設定為 “application/x-www-form-urlencoded”。

如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是空字串,則會附加至 address

此方法會使用 STOR 命令來上傳 FTP 資源。 如果是 HTTP 資源,則會使用 POST 方法。

注意

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於

UploadValues(Uri, NameValueCollection)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
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”。

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

UploadValues 方法會將 NameValueCollection 傳送至伺服器。 這個方法會在上傳數據時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中一個 UploadValuesAsync 方法。

如果伺服器無法了解基礎要求,基礎通訊協定類別會決定會發生什麼情況。 一般而言,會擲回 WebException,並將 Status 屬性設定為表示錯誤。

如果 content-type 標頭是 nullUploadValues 方法會將它設定為 “application/x-www-form-urlencoded”。

如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是空字串,則會附加至 address

此方法會使用 STOR 命令來上傳 FTP 資源。 如果是 HTTP 資源,則會使用 POST 方法。

注意

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於

UploadValues(String, String, NameValueCollection)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
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))

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

UploadValues 方法會使用 method 參數中指定的 方法,將 NameValueCollection 傳送至資源,並從伺服器傳回任何回應。 這個方法會在上傳數據時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中一個 UploadValuesAsync 方法。

如果 Content-type 標頭是 nullUploadValues 方法會將它設定為 application/x-www-form-urlencoded

如果 method 參數指定伺服器無法理解的動詞,基礎通訊協定類別會決定會發生什麼事。 一般而言,會擲回 WebException,並將 Status 屬性設定為表示錯誤。

如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是空字串,則會附加至 address

注意

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於

UploadValues(Uri, String, NameValueCollection)

來源:
WebClient.cs
來源:
WebClient.cs
來源:
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

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

UploadValues 方法會使用 method 參數中指定的 方法,將 NameValueCollection 傳送至資源,並從伺服器傳回任何回應。 這個方法會在上傳數據時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中一個 UploadValuesAsync 方法。

如果 Content-type 標頭是 nullUploadValues 方法會將它設定為 application/x-www-form-urlencoded

如果 method 參數指定伺服器無法理解的動詞,基礎通訊協定類別會決定會發生什麼事。 一般而言,會擲回 WebException,並將 Status 屬性設定為表示錯誤。

如果 BaseAddress 屬性不是空字串 (“”) 且 address 不包含絕對 URI,address 必須是與 BaseAddress 結合的相對 URI,才能形成所要求數據的絕對 URI。 如果 QueryString 屬性不是空字串,則會附加至 address

注意

當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於