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

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

打开流时出错。

示例

下面的代码示例从命令行读取数据,并使用 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.")

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

OpenWrite 方法返回用于将数据发送到资源的可写流。 此方法在打开流时阻止。 若要在等待流时继续执行,请使用 OpenWriteAsync 方法之一。

如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 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

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

打开流时出错。

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

OpenWrite 方法返回用于将数据发送到资源的可写流。 此方法在打开流时阻止。 若要在等待流时继续执行,请使用 OpenWriteAsync 方法之一。

如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 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 和 STOR for ftp。

返回

用于将数据写入资源的 Stream

例外

address 参数 null

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

打开流时出错。

示例

下面的代码示例从命令行读取数据,并使用 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;

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.")

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

OpenWrite 方法返回用于将数据发送到资源的可写流。 使用 method 参数中指定的方法发出基础请求。 关闭流时,数据将发送到服务器。 此方法在打开流时阻止。 若要在等待流时继续执行,请使用 OpenWriteAsync 方法之一。

如果 method 参数指定服务器无法理解的方法,基础协议类将确定发生的情况。 通常,会引发 WebException,并将 Status 属性设置为指示错误。

如果 BaseAddress 属性不是空字符串(“”)且 address 未指定绝对地址,address 必须是与 BaseAddress 相结合的相对 URI,以形成所请求数据的绝对 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 和 STOR for ftp。

返回

用于将数据写入资源的 Stream

例外

address 参数 null

组合 BaseAddress而形成的 URI 无效,address 无效。

-或-

打开流时出错。

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

OpenWrite 方法返回用于将数据发送到资源的可写流。 此方法在打开流时阻止。 若要在等待流时继续执行,请使用 OpenWriteAsync 方法之一。

如果 BaseAddress 属性不是空字符串(“”)且 address 不包含绝对 URI,address 必须是与 BaseAddress 相结合的相对 URI,才能形成所请求数据的绝对 URI。 如果 QueryString 属性不是空字符串,则会将其追加到 address

注意

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

适用于