WebClient.UploadString 方法

定義

將指定的字串上傳至指定的資源。Uploads the specified string to the specified resource.

多載

UploadString(String, String)

使用 POST 方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the POST method.

UploadString(Uri, String)

使用 POST 方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the POST method.

UploadString(String, String, String)

使用指定的方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the specified method.

UploadString(Uri, String, String)

使用指定的方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the specified method.

UploadString(String, String)

使用 POST 方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the POST method.

public:
 System::String ^ UploadString(System::String ^ address, System::String ^ data);
public string UploadString (string address, string data);
member this.UploadString : string * string -> string
Public Function UploadString (address As String, data As String) As String

參數

address
String

要接收字串之資源的 URI。The URI of the resource to receive the string. 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。For Http resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page.

data
String

要上傳的字串。The string to be uploaded.

傳回

String,其中包含由伺服器傳送的回應。A String containing the response sent by the server.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

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

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

範例

下列程式碼範例將示範如何呼叫這個方法。The following code example demonstrates calling this method.

void UploadString( String^ address )
{
   String^ data = "Time = 12:00am temperature = 50";
   WebClient^ client = gcnew WebClient;
   
   // Optionally specify an encoding for uploading and downloading strings.
   client->Encoding = System::Text::Encoding::UTF8;
   
   // Upload the data.
   String^ reply = client->UploadString( address, data );
   
   // Disply the server's response.
   Console::WriteLine( reply );
}


public static void UploadString (string address)
{
    string data = "Time = 12:00am temperature = 50";
    WebClient client = new WebClient ();
    // Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8;
    // Upload the data.
    string reply = client.UploadString (address, data);
    // Disply the server's response.
    Console.WriteLine (reply);
}

Public Shared Sub UploadString(ByVal address As String)

    Dim data As String = "Time = 12:00am temperature = 50"
    Dim client As WebClient = New WebClient()
    '  Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8
    '  Upload the data.
    Dim reply As String = client.UploadString(address, data)
    '  Disply the server's response.
    Console.WriteLine(reply)
End Sub

備註

上傳字串之前,這個方法會使用 Encoding 屬性中指定的編碼方式,將它轉換成 Byte 陣列。Before uploading the string, this method converts it to a Byte array using the encoding specified in the Encoding property. 這個方法會在傳輸字串時封鎖。This method blocks while the string is transmitted. 若要在等候伺服器回應時傳送字串並繼續執行,請使用其中一個 UploadStringAsync 方法。To send a string and continue executing while waiting for the server's response, use one of the UploadStringAsync 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.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。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.

UploadString(Uri, String)

使用 POST 方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the POST method.

public:
 System::String ^ UploadString(Uri ^ address, System::String ^ data);
public string UploadString (Uri address, string data);
member this.UploadString : Uri * string -> string
Public Function UploadString (address As Uri, data As String) As String

參數

address
Uri

要接收字串之資源的 URI。The URI of the resource to receive the string. 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。For Http resources, this URI must identify a resource that can accept a request sent with the POST method, such as a script or ASP page.

data
String

要上傳的字串。The string to be uploaded.

傳回

String,其中包含由伺服器傳送的回應。A String containing the response sent by the server.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

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

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

備註

上傳字串之前,這個方法會使用 Encoding 屬性中指定的編碼方式,將它轉換成 Byte 陣列。Before uploading the string, this method converts it to a Byte array using the encoding specified in the Encoding property. 這個方法會在傳輸字串時封鎖。This method blocks while the string is transmitted. 若要在等候伺服器回應時傳送字串並繼續執行,請使用其中一個 UploadStringAsync 方法。To send a string and continue executing while waiting for the server's response, use one of the UploadStringAsync 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.

這個方法會使用 STOR 命令來上傳 FTP 資源。This method uses the STOR command to upload an FTP resource. 若為 HTTP 資源,則會使用 POST 方法。For an HTTP resource, the POST method is used.

注意

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。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.

UploadString(String, String, String)

使用指定的方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the specified method.

public:
 System::String ^ UploadString(System::String ^ address, System::String ^ method, System::String ^ data);
public string UploadString (string address, string method, string data);
member this.UploadString : string * string * string -> string
Public Function UploadString (address As String, method As String, data As String) As String

參數

address
String

要接收字串之資源的 URI。The URI of the resource to receive the string. 這個 URI 必須識別出可以接受以 method 方法傳送之要求的資源。This URI must identify a resource that can accept a request sent with the method method.

method
String

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

data
String

要上傳的字串。The string to be uploaded.

傳回

String,其中包含由伺服器傳送的回應。A String containing the response sent by the server.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

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

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

-或--or- 無法使用 method 傳送內容。method cannot be used to send content.

範例

下列程式碼範例將示範如何呼叫這個方法。The following code example demonstrates calling this method.

void PostString( String^ address )
{
   String^ data = "Time = 12:00am temperature = 50";
   String^ method = "POST";
   WebClient^ client = gcnew WebClient;
   String^ reply = client->UploadString( address, method, data );
   Console::WriteLine( reply );
}


public static void PostString (string address)
{
    string data = "Time = 12:00am temperature = 50";
    string method = "POST";
    WebClient client = new WebClient ();
    string reply = client.UploadString (address, method, data);

    Console.WriteLine (reply);
}

Public Shared Sub PostString(ByVal address As String)

    Dim data As String = "Time = 12:00am temperature = 50"
    Dim method As String = "POST"
    Dim client As WebClient = New WebClient()
    Dim reply As String = client.UploadString(address, method, data)

    Console.WriteLine(reply)
End Sub

備註

上傳字串之前,這個方法會使用 Encoding 屬性中指定的編碼方式,將它轉換成 Byte 陣列。Before uploading the string, this method converts it to a Byte array using the encoding specified in the Encoding property. 這個方法會在傳輸字串時封鎖。This method blocks while the string is transmitted. 若要在等候伺服器回應時傳送字串並繼續執行,請使用其中一個 UploadStringAsync 方法。To send a string and continue executing while waiting for the server's response, use one of the UploadStringAsync 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.

UploadString(Uri, String, String)

使用指定的方法,將指定的字串上載至指定的資源。Uploads the specified string to the specified resource, using the specified method.

public:
 System::String ^ UploadString(Uri ^ address, System::String ^ method, System::String ^ data);
public string UploadString (Uri address, string method, string data);
member this.UploadString : Uri * string * string -> string
Public Function UploadString (address As Uri, method As String, data As String) As String

參數

address
Uri

要接收字串之資源的 URI。The URI of the resource to receive the string. 這個 URI 必須識別出可以接受以 method 方法傳送之要求的資源。This URI must identify a resource that can accept a request sent with the method method.

method
String

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

data
String

要上傳的字串。The string to be uploaded.

傳回

String,其中包含由伺服器傳送的回應。A String containing the response sent by the server.

例外狀況

address 參數為 nullThe address parameter is null.

-或--or- data 參數為 nullThe data parameter is null.

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

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

-或--or- 無法使用 method 傳送內容。method cannot be used to send content.

備註

上傳字串之前,這個方法會使用 Encoding 屬性中指定的編碼方式,將它轉換成 Byte 陣列。Before uploading the string, this method converts it to a Byte array using the encoding specified in the Encoding property. 這個方法會在傳輸字串時封鎖。This method blocks while the string is transmitted. 若要在等候伺服器回應時傳送字串並繼續執行,請使用其中一個 UploadStringAsync 方法。To send a string and continue executing while waiting for the server's response, use one of the UploadStringAsync 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.

適用於