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 來取得寫入資料的資料流程。 傳 OpenWrite 回的 會在 Stream 傳送資料之後關閉。

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 中的網路追蹤

適用於