WebClient.UploadData 方法

定義

將資料緩衝區上載至含有指定之 URI 的資源。Uploads a data buffer to a resource with the specified URI.

多載

UploadData(String, Byte[])

將資料緩衝區上載至由 URI 識別的資源。Uploads a data buffer to a resource identified by a URI.

UploadData(Uri, Byte[])

將資料緩衝區上載至由 URI 識別的資源。Uploads a data buffer to a resource identified by a URI.

UploadData(String, String, Byte[])

使用指定的方法,將資料緩衝區上載至指定的資源。Uploads a data buffer to the specified resource, using the specified method.

UploadData(Uri, String, Byte[])

使用指定的方法,將資料緩衝區上載至指定的資源。Uploads a data buffer to the specified resource, using the specified method.

UploadData(String, Byte[])

將資料緩衝區上載至由 URI 識別的資源。Uploads a data buffer to a resource identified by a 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。The URI of the resource to receive the data.

data
Byte[]

要傳送至資源的資料緩衝區。The data buffer 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.

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

-或--or- datanulldata is null.

-或--or- 傳送資料時發生錯誤。An error occurred while sending the data.

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

範例

下列程式碼範例會將從主控台輸入的字串轉換為 Byte 陣列,並使用 UploadData將陣列張貼至指定的伺服器。The following code example converts a string entered from the console to a Byte array and posts the array to the specified server using UploadData. 伺服器的任何回應都會顯示在主控台中。Any response from the server is displayed to 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;
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 方法會將資料緩衝區傳送至資源。The UploadData method sends a data buffer to a resource.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used. 如果伺服器無法辨識基礎要求,則基礎通訊協定類別會決定發生什麼事。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.

UploadData 方法會將 data 的內容傳送到伺服器,而不需要進行編碼。The UploadData method sends the content of data to the server without encoding it. 這個方法會在上傳資料時封鎖。This method blocks while uploading the data. 若要在等候伺服器回應時繼續執行,請使用其中一種 UploadDataAsync 方法。To continue executing while waiting for the server's response, use one of the UploadDataAsync methods.

如果 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.

UploadData(Uri, Byte[])

將資料緩衝區上載至由 URI 識別的資源。Uploads a data buffer to a resource identified by a 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。The URI of the resource to receive the data.

data
Byte[]

要傳送至資源的資料緩衝區。The data buffer 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.

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

-或--or- datanulldata is null.

-或--or- 傳送資料時發生錯誤。An error occurred while sending the data.

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

備註

UploadData 方法會將資料緩衝區傳送至資源。The UploadData method sends a data buffer to a resource.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used. 如果伺服器無法辨識基礎要求,則基礎通訊協定類別會決定發生什麼事。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.

UploadData 方法會將 data 的內容傳送到伺服器,而不需要進行編碼。The UploadData method sends the content of data to the server without encoding it. 這個方法會在上傳資料時封鎖。This method blocks while uploading the data. 若要在等候伺服器回應時繼續執行,請使用其中一種 UploadDataAsync 方法。To continue executing while waiting for the server's response, use one of the UploadDataAsync methods.

如果 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.

UploadData(String, String, Byte[])

使用指定的方法,將資料緩衝區上載至指定的資源。Uploads a data buffer to the specified resource, using the specified method.

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);
member this.UploadData : string * string * byte[] -> byte[]
Public Function UploadData (address As String, method As String, data As Byte()) As Byte()

參數

address
String

要接收資料之資源的 URI。The URI of the resource to receive the data.

method
String

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

data
Byte[]

要傳送至資源的資料緩衝區。The data buffer 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.

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

-或--or- datanulldata is null.

-或--or- 上載資料時發生錯誤。An error occurred while uploading the data.

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

範例

下列程式碼範例會將從主控台輸入的字串轉換成位元組陣列,並使用 UploadData將陣列張貼至指定的伺服器。The following code example converts a string entered from the console into a byte array and posts the array to the specified server using UploadData. 伺服器的任何回應都會顯示在主控台中。Any response from the server is displayed to the console.

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

UploadData 方法會將 data 的內容傳送到伺服器,而不需要進行編碼。The UploadData method sends the content of data to the server without encoding it.

如果 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.

UploadData(Uri, String, Byte[])

使用指定的方法,將資料緩衝區上載至指定的資源。Uploads a data buffer to the specified resource, using the specified method.

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);
member this.UploadData : Uri * string * byte[] -> byte[]
Public Function UploadData (address As Uri, method As String, data As Byte()) As Byte()

參數

address
Uri

要接收資料之資源的 URI。The URI of the resource to receive the data.

method
String

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

data
Byte[]

要傳送至資源的資料緩衝區。The data buffer 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.

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

-或--or- datanulldata is null.

-或--or- 上載資料時發生錯誤。An error occurred while uploading the data.

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

備註

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

UploadData 方法會將 data 的內容傳送到伺服器,而不需要進行編碼。The UploadData method sends the content of data to the server without encoding it.

如果 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.

適用於