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

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

開啟資料流時發生錯誤。

-或-

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 則必須是結合 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

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

開啟資料流時發生錯誤。

-或-

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 則必須是結合 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

-或-

開啟資料流時發生錯誤。

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

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 會使用 參數中指定的 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)

來源:
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

-或-

開啟資料流時發生錯誤。

-或-

沒有來自裝載 (Host) 資源之伺服器的回應。

-或-

Content-type 標頭值不是 null,也不是 application/x-www-form-urlencoded

備註

方法 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 中的網路追蹤

適用於