次の方法で共有


WebClient.OpenWrite メソッド

指定した URI を持つリソースへデータを書き込むストリームを開きます。

オーバーロードの一覧

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

[Visual Basic] Overloads Public Function OpenWrite(String) As Stream

[C#] public Stream OpenWrite(string);

[C++] public: Stream* OpenWrite(String*);

[JScript] public function OpenWrite(String) : Stream;

指定したメソッドを使用して、指定したリソースにデータを書き込むためのストリームを開きます。

[Visual Basic] Overloads Public Function OpenWrite(String, String) As Stream

[C#] public Stream OpenWrite(string, string);

[C++] public: Stream* OpenWrite(String*, String*);

[JScript] public function OpenWrite(String, String) : Stream;

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、OpenWrite のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

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

[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();

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

[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();

Console::WriteLine(S"Uploading to {0} ...",  uriString);
Stream*  postStream = myWebClient->OpenWrite(uriString, S"POST");
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

WebClient クラス | WebClient メンバ | System.Net 名前空間