WebClient.UploadData 方法

定義

將資料緩衝區上載至含有指定之 URI 的資源。

多載

UploadData(String, Byte[])

將資料緩衝區上載至由 URI 識別的資源。

UploadData(Uri, Byte[])

將資料緩衝區上載至由 URI 識別的資源。

UploadData(String, String, Byte[])

使用指定的方法,將資料緩衝區上載至指定的資源。

UploadData(Uri, String, Byte[])

使用指定的方法,將資料緩衝區上載至指定的資源。

UploadData(String, Byte[])

來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs

將資料緩衝區上載至由 URI 識別的資源。

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

參數

address
String

要接收資料之資源的 URI。

data
Byte[]

要傳送至資源的資料緩衝區。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

datanull

-或-

傳送資料時發生錯誤。

-或-

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

範例

下列程式碼範例會將從主控台輸入的字串轉換為 Byte 陣列,並使用 UploadData 將陣列張貼至指定的伺服器。 伺服器的任何回應都會顯示至主控台。

Console::Write( "\nPlease enter the URI to post data to: " );
String^ uriString = Console::ReadLine();
// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
// Apply ASCII Encoding to obtain the String* as a Byte array.
array<Byte>^ postArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

//UploadData implicitly sets HTTP POST as the request method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, postArray );

// Decode and display the response.
Console::WriteLine( "\nResponse received was: {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();
    Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
    string postData = Console.ReadLine();
    // Apply ASCII Encoding to obtain the string as a byte array.
    byte[] postArray = Encoding.ASCII.GetBytes(postData);
    Console.WriteLine("Uploading to {0} ...",  uriString);							
 myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

    //UploadData implicitly sets HTTP POST as the request method.
    byte[] responseArray = myWebClient.UploadData(uriString,postArray);

    // Decode and display the response.
    Console.WriteLine("\nResponse received was :{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()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
' Apply ASCII Encoding to obtain the string as a byte array.
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' UploadData implicitly sets HTTP POST as the request method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, postArray)

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

備註

方法會將 UploadData 資料緩衝區傳送至資源。

此方法會使用 STOR 命令來上傳 FTP 資源。 針對 HTTP 資源,會使用 POST 方法。 如果伺服器無法瞭解基礎要求,基礎通訊協定類別會決定會發生什麼情況。 一般而言, WebException 會擲回 Status 屬性集來表示錯誤。

方法 UploadData 會將 的內容 data 傳送至伺服器,而不需編碼。 這個方法會在上傳資料時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadDataAsync 一種方法。

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

注意

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

適用於

UploadData(Uri, Byte[])

來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs

將資料緩衝區上載至由 URI 識別的資源。

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

參數

address
Uri

要接收資料之資源的 URI。

data
Byte[]

要傳送至資源的資料緩衝區。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

datanull

-或-

傳送資料時發生錯誤。

-或-

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

備註

方法會將 UploadData 資料緩衝區傳送至資源。

此方法會使用 STOR 命令來上傳 FTP 資源。 針對 HTTP 資源,會使用 POST 方法。 如果伺服器無法瞭解基礎要求,基礎通訊協定類別會決定會發生什麼情況。 一般而言, WebException 會擲回 Status 屬性集來表示錯誤。

方法 UploadData 會將 的內容 data 傳送至伺服器,而不需編碼。 這個方法會在上傳資料時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadDataAsync 一種方法。

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

注意

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

適用於

UploadData(String, String, Byte[])

來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs

使用指定的方法,將資料緩衝區上載至指定的資源。

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

參數

address
String

要接收資料之資源的 URI。

method
String

傳送資料給資源所使用的 HTTP 方法。 如果是 Null,預設的 HTTP 為 POST,而 FTP 則為 STOR。

data
Byte[]

要傳送至資源的資料緩衝區。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

datanull

-或-

上載資料時發生錯誤。

-或-

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

範例

下列程式碼範例會將從主控台輸入的字串轉換成位元組陣列,並使用 將陣列張貼至指定的伺服器 UploadData 。 伺服器的任何回應都會顯示至主控台。

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, http://www.contoso.com}: " );
uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

// Displays the headers in the request
Console::Write( "Resulting Request Headers: ");
Console::WriteLine(myWebClient->Headers);

// Apply ASCII Encoding to obtain the String^ as a Byte array.
array<Byte>^ byteArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
// Upload the input String* using the HTTP 1.0 POST method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, "POST", byteArray );
// Decode and display the response.
Console::WriteLine( "\nResponse received was {0}",
   Encoding::ASCII->GetString( responseArray ) );
   string uriString;
       Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
       uriString = Console.ReadLine();

       // Create a new WebClient instance.
       WebClient myWebClient = new WebClient();
       Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
       string postData = Console.ReadLine();
       myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // Apply ASCII Encoding to obtain the string as a byte array.

       byte[] byteArray = Encoding.ASCII.GetBytes(postData);
       Console.WriteLine("Uploading to {0} ...",  uriString);						
       // Upload the input string using the HTTP 1.0 POST method.
       byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
       
       // Decode and display the response.
       Console.WriteLine("\nResponse received was {0}",
       Encoding.ASCII.GetString(responseArray));
                 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' Display the headers in the request
Console.Write("Resulting Request Headers: ")
Console.Writeline(myWebClient.Headers.ToString())

' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

備註

方法 UploadData 會使用 參數中指定的 method HTTP 方法,將資料緩衝區傳送至資源,並從伺服器傳回任何回應。 這個方法會在上傳資料時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadDataAsync 一種方法。

方法 UploadData 會將 的內容 data 傳送至伺服器,而不需編碼。

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

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

注意

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

適用於

UploadData(Uri, String, Byte[])

來源:
WebClient.cs
來源:
WebClient.cs
來源:
WebClient.cs

使用指定的方法,將資料緩衝區上載至指定的資源。

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

參數

address
Uri

要接收資料之資源的 URI。

method
String

傳送資料給資源所使用的 HTTP 方法。 如果是 Null,預設的 HTTP 為 POST,而 FTP 則為 STOR。

data
Byte[]

要傳送至資源的資料緩衝區。

傳回

Byte[]

Byte 陣列,包含來自資源的回應主體。

例外狀況

address 參數為 null

合併 BaseAddressaddress 而形成的 URI 是無效的。

-或-

datanull

-或-

上載資料時發生錯誤。

-或-

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

備註

方法 UploadData 會使用 參數中指定的 method HTTP 方法,將資料緩衝區傳送至資源,並從伺服器傳回任何回應。 這個方法會在上傳資料時封鎖。 若要在等候伺服器的回應時繼續執行,請使用其中 UploadDataAsync 一種方法。

方法 UploadData 會將 的內容 data 傳送至伺服器,而不需編碼。

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

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

注意

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

適用於