次の方法で共有


WebClient.OpenWrite メソッド (String)

指定したリソースにデータを書き込むためのストリームを開きます。

Overloads Public Function OpenWrite( _
   ByVal address As String _) As Stream
[C#]
public Stream OpenWrite(stringaddress);
[C++]
public: Stream* OpenWrite(String* address);
[JScript]
public function OpenWrite(
   address : String) : Stream;

パラメータ

  • address
    データを受信するリソースの URI。

戻り値

リソースにデータを書き込むために使用する Stream

例外

例外の種類 条件
WebException BaseAddress および address を組み合わせて形成された URI が無効です。

または

ストリームのオープン中にエラーが発生しました。

解説

OpenWrite メソッドは、リソースにデータを送信するために使用する書き込み可能ストリームを返します。基になる要求は、POST メソッドで作成されます。

BaseAddress プロパティが空でない場合、 address は、相対 URI である必要があります。この相対 URI は、 BaseAddress と結合されて、要求されたデータの絶対 URI を形成します。 QueryString プロパティが空でない場合は、 address に追加されます。

使用例

[Visual Basic, C#, C++] コマンド ラインからデータを読み取り、 OpenWrite を使用して、データを書き込むためのストリームを取得する例を次に示します。データの送信後、 OpenWrite で返された Stream は閉じます。

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

[C#] 
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.");

[C++] 
String* uriString;
Console::Write(S"\nPlease enter the URI to post data to : ");
uriString = Console::ReadLine();
Console::WriteLine(S"\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(S"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(S"\nSuccessfully posted the data.");

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

WebClient クラス | WebClient メンバ | System.Net 名前空間 | WebClient.OpenWrite オーバーロードの一覧