WebClient.OpenWrite 方法

定義

開啟資料流,用於將資料寫入至具有指定之 URI 的資源。

多載

OpenWrite(String)

開啟資料流以將資料寫入至指定的資源。

OpenWrite(Uri)

開啟資料流以將資料寫入至指定的資源。

OpenWrite(String, String)

使用指定的方法開啟資料流,以將資料寫入至指定的資源。

OpenWrite(Uri, String)

使用指定的方法開啟資料流,以將資料寫入至指定的資源。

OpenWrite(String)

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

開啟資料流以將資料寫入至指定的資源。

public:
 System::IO::Stream ^ OpenWrite(System::String ^ address);
public System.IO.Stream OpenWrite (string address);
member this.OpenWrite : string -> System.IO.Stream
Public Function OpenWrite (address As String) As Stream

參數

address
String

要接收資料之資源的 URI。

傳回

Stream,用來將資料寫入至資源。

例外狀況

address 參數為 null

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

-或-

開啟資料流時發生錯誤。

範例

下列程式代碼範例會從命令行讀取數據,並使用 OpenWrite 來取得數據流來寫入數據。 傳 Stream 回的 OpenWrite 會在傳送數據之後關閉。

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to: " );
uriString = Console::ReadLine();
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
// Apply Ascii Encoding to obtain an array of bytes.
array<Byte>^ postArray = Encoding::ASCII->GetBytes( postData );

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;

// postStream implicitly sets HTTP POST as the request method.
Console::WriteLine( "Uploading to {0} ...", uriString );
Stream^ postStream = myWebClient->OpenWrite( uriString );

postStream->Write( postArray, 0, postArray->Length );

// Close the stream and release resources.
postStream->Close();

Console::WriteLine( "\nSuccessfully posted the data." );
string uriString;
Console.Write("\nPlease enter the URI to post data to : ");
uriString = Console.ReadLine();
Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
string postData = Console.ReadLine();
// Apply Ascii Encoding to obtain an array of bytes. 
byte[] postArray = Encoding.ASCII.GetBytes(postData);

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// postStream implicitly sets HTTP POST as the request method.
Console.WriteLine("Uploading to {0} ...",  uriString);							Stream postStream = myWebClient.OpenWrite(uriString);

postStream.Write(postArray,0,postArray.Length);

// Close the stream and release resources.
postStream.Close();

Console.WriteLine("\nSuccessfully posted the data.");
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
uriString = Console.ReadLine()
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 an array of bytes .
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)

' Create a new WebClient instance.
Dim myWebClient As New WebClient()

Console.WriteLine("Uploading to {0} ...", uriString)

' OpenWrite implicitly sets HTTP POST as the request method.
Dim postStream As Stream = myWebClient.OpenWrite(uriString)
postStream.Write(postArray, 0, postArray.Length)

' Close the stream and release resources.
postStream.Close()

Console.WriteLine(ControlChars.Cr + "Successfully posted the data.")

備註

方法 OpenWrite 會傳回可寫入的數據流,用來將數據傳送至資源。 這個方法會在開啟數據流時封鎖。 若要在等候數據流時繼續執行,請使用其中 OpenWriteAsync 一種方法。

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

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

注意

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

適用於

OpenWrite(Uri)

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

開啟資料流以將資料寫入至指定的資源。

public:
 System::IO::Stream ^ OpenWrite(Uri ^ address);
public System.IO.Stream OpenWrite (Uri address);
member this.OpenWrite : Uri -> System.IO.Stream
Public Function OpenWrite (address As Uri) As Stream

參數

address
Uri

要接收資料之資源的 URI。

傳回

Stream,用來將資料寫入至資源。

例外狀況

address 參數為 null

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

-或-

開啟資料流時發生錯誤。

備註

方法 OpenWrite 會傳回可寫入的數據流,用來將數據傳送至資源。 這個方法會在開啟數據流時封鎖。 若要在等候數據流時繼續執行,請使用其中 OpenWriteAsync 一種方法。

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

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

注意

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

適用於

OpenWrite(String, String)

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

使用指定的方法開啟資料流,以將資料寫入至指定的資源。

public:
 System::IO::Stream ^ OpenWrite(System::String ^ address, System::String ^ method);
public System.IO.Stream OpenWrite (string address, string? method);
public System.IO.Stream OpenWrite (string address, string method);
member this.OpenWrite : string * string -> System.IO.Stream
Public Function OpenWrite (address As String, method As String) As Stream

參數

address
String

要接收資料之資源的 URI。

method
String

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

傳回

Stream,用來將資料寫入至資源。

例外狀況

address 參數為 null

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

-或-

開啟資料流時發生錯誤。

範例

下列程式代碼範例會從命令行讀取數據,並使用 OpenWrite 來取得用來寫入數據的數據流。 所 Stream 傳回的 OpenWrite 必須關閉才能傳送數據。

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to: " );
uriString = Console::ReadLine();
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
// Apply ASCII encoding to obtain an array of bytes .
array<Byte>^ postArray = Encoding::ASCII->GetBytes( postData );

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;

Console::WriteLine( "Uploading to {0} ...", uriString );
Stream^ postStream = myWebClient->OpenWrite( uriString, "POST" );
postStream->Write( postArray, 0, postArray->Length );

// Close the stream and release resources.
postStream->Close();
Console::WriteLine( "\nSuccessfully posted the data." );
string uriString;
Console.Write("\nPlease enter the URI to post data to : ");
uriString = Console.ReadLine();
Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
string postData = Console.ReadLine();
// Apply ASCII encoding to obtain an array of bytes .
byte[] postArray = Encoding.ASCII.GetBytes(postData);

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

Console.WriteLine("Uploading to {0} ...",  uriString);						
Stream postStream = myWebClient.OpenWrite(uriString,"POST");
postStream.Write(postArray,0,postArray.Length);

// Close the stream and release resources.
postStream.Close();
Console.WriteLine("\nSuccessfully posted the data.");
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
uriString = Console.ReadLine()
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 an array of bytes.
Dim postArray As Byte() = Encoding.ASCII.GetBytes(postData)

' Create a new WebClient instance.
Dim myWebClient As New WebClient()

Console.WriteLine("Uploading to {0} ...", uriString)
Dim postStream As Stream = myWebClient.OpenWrite(uriString, "POST")

postStream.Write(postArray, 0, postArray.Length)

' Close the stream and release resources.
postStream.Close()

Console.WriteLine(ControlChars.Cr + "Successfully posted the data.")

備註

方法 OpenWrite 會傳回可寫入的數據流,用來將數據傳送至資源。 基礎要求是以 參數中指定的 method 方法進行。 當您關閉數據流時,數據會傳送至伺服器。 這個方法會在開啟數據流時封鎖。 若要在等候數據流時繼續執行,請使用其中 OpenWriteAsync 一種方法。

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

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

注意

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

適用於

OpenWrite(Uri, String)

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

使用指定的方法開啟資料流,以將資料寫入至指定的資源。

public:
 System::IO::Stream ^ OpenWrite(Uri ^ address, System::String ^ method);
public System.IO.Stream OpenWrite (Uri address, string? method);
public System.IO.Stream OpenWrite (Uri address, string method);
member this.OpenWrite : Uri * string -> System.IO.Stream
Public Function OpenWrite (address As Uri, method As String) As Stream

參數

address
Uri

要接收資料之資源的 URI。

method
String

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

傳回

Stream,用來將資料寫入至資源。

例外狀況

address 參數為 null

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

-或-

開啟資料流時發生錯誤。

備註

方法 OpenWrite 會傳回可寫入的數據流,用來將數據傳送至資源。 這個方法會在開啟數據流時封鎖。 若要在等候數據流時繼續執行,請使用其中 OpenWriteAsync 一種方法。

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

注意

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

適用於