WebClient.UploadString 方法

定義

將指定的字串上傳至指定的資源。

多載

UploadString(String, String)

使用 POST 方法,將指定的字串上載至指定的資源。

UploadString(Uri, String)

使用 POST 方法,將指定的字串上載至指定的資源。

UploadString(String, String, String)

使用指定的方法,將指定的字串上載至指定的資源。

UploadString(Uri, String, String)

使用指定的方法,將指定的字串上載至指定的資源。

UploadString(String, String)

使用 POST 方法,將指定的字串上載至指定的資源。

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。 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。

data
String

要上傳的字串。

傳回

String

String,其中包含由伺服器傳送的回應。

例外狀況

address 參數為 null

-或-

data 參數為 null

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

-或-

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

範例

下列程式碼範例示範如何呼叫這個方法。

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);
    // Display 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 轉換成陣列。 這個方法會在傳輸字串時封鎖。 若要在等候伺服器的回應時傳送字串並繼續執行,請使用其中 UploadStringAsync 一種方法。

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

此方法會使用 STOR 命令來上傳 FTP 資源。 針對 HTTP 資源,會使用 POST 方法。

注意

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

適用於

UploadString(Uri, String)

使用 POST 方法,將指定的字串上載至指定的資源。

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。 對於 HTTP 資源而言,這個 URI 必須識別出可以接受使用 POST 方法傳送之要求的資源,例如指令碼或 ASP 網頁。

data
String

要上傳的字串。

傳回

String

String,其中包含由伺服器傳送的回應。

例外狀況

address 參數為 null

-或-

data 參數為 null

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

-或-

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

備註

上傳字串之前,這個方法會使用 屬性中指定的 Encoding 編碼,將它 Byte 轉換成陣列。 這個方法會在傳輸字串時封鎖。 若要在等候伺服器的回應時傳送字串並繼續執行,請使用其中 UploadStringAsync 一種方法。

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

此方法會使用 STOR 命令來上傳 FTP 資源。 針對 HTTP 資源,會使用 POST 方法。

注意

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

適用於

UploadString(String, String, String)

使用指定的方法,將指定的字串上載至指定的資源。

public:
 System::String ^ UploadString(System::String ^ address, System::String ^ method, System::String ^ data);
public string UploadString (string address, string? method, 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。 這個 URI 必須識別出可以接受以 method 方法傳送之要求的資源。

method
String

將字串傳送至資源時所使用的 HTTP 方法。 如果是 Null,預設的 HTTP 為 POST,而 FTP 則為 STOR。

data
String

要上傳的字串。

傳回

String

String,其中包含由伺服器傳送的回應。

例外狀況

address 參數為 null

-或-

data 參數為 null

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

-或-

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

-或-

無法使用 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 轉換成陣列。 這個方法會在傳輸字串時封鎖。 若要在等候伺服器的回應時傳送字串並繼續執行,請使用其中 UploadStringAsync 一種方法。

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

注意

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

適用於

UploadString(Uri, String, String)

使用指定的方法,將指定的字串上載至指定的資源。

public:
 System::String ^ UploadString(Uri ^ address, System::String ^ method, System::String ^ data);
public string UploadString (Uri address, string? method, 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。 這個 URI 必須識別出可以接受以 method 方法傳送之要求的資源。

method
String

將字串傳送至資源時所使用的 HTTP 方法。 如果是 Null,預設的 HTTP 為 POST,而 FTP 則為 STOR。

data
String

要上傳的字串。

傳回

String

String,其中包含由伺服器傳送的回應。

例外狀況

address 參數為 null

-或-

data 參數為 null

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

-或-

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

-或-

無法使用 method 傳送內容。

備註

上傳字串之前,這個方法會使用 屬性中指定的 Encoding 編碼,將它 Byte 轉換成陣列。 這個方法會在傳輸字串時封鎖。 若要在等候伺服器的回應時傳送字串並繼續執行,請使用其中 UploadStringAsync 一種方法。

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

注意

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

適用於