共用方式為


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

-或-

傳送數據時發生錯誤。

-或-

裝載資源的伺服器沒有回應。

範例

下列程式代碼範例會將從控制台輸入的字串轉換成 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))

備註

謹慎

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

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

-或-

傳送數據時發生錯誤。

-或-

裝載資源的伺服器沒有回應。

備註

謹慎

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

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

-或-

上傳數據時發生錯誤。

-或-

裝載資源的伺服器沒有回應。

範例

下列程式代碼範例會將從控制台輸入的字串轉換成位元組陣列,並使用 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))

備註

謹慎

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

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

-或-

上傳數據時發生錯誤。

-或-

裝載資源的伺服器沒有回應。

備註

謹慎

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

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

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

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

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

注意

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

適用於