WebClient.OpenWrite 方法

定义

打开一个流以将数据写入具有指定 URI 的资源。

重载

OpenWrite(String)

打开一个流以将数据写入指定的资源。

OpenWrite(Uri)

打开一个流以将数据写入指定的资源。

OpenWrite(String, String)

打开一个流以使用指定的方法向指定的资源写入数据。

OpenWrite(Uri, String)

打开一个流以使用指定的方法将数据写入指定的资源。

OpenWrite(String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
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则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

OpenWrite(Uri)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
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则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

此方法使用 STOR 命令上传 FTP 资源。 对于 HTTP 资源,使用 POST 方法。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

OpenWrite(String, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
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则必须是一个相对 URI,与 BaseAddress 组合在一起以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于

OpenWrite(Uri, String)

Source:
WebClient.cs
Source:
WebClient.cs
Source:
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则必须是一个相对 URI,与 BaseAddress 组合以构成所请求数据的绝对 URI。 QueryString如果 属性不是空字符串,则会将其追加到 address

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

适用于