次の方法で共有


FileWebRequest.GetRequestStream メソッド

ファイル システム リソースにデータを書き込む Stream インスタンスを返します。

Overrides Public Function GetRequestStream() As Stream
[C#]
public override Stream GetRequestStream();
[C++]
public: Stream* GetRequestStream();
[JScript]
public override function GetRequestStream() : Stream;

戻り値

ファイル システム リソースにデータを書き込む Stream

例外

例外の種類 条件
WebException 要求がタイムアウトしました。

解説

GetRequestStream メソッドは、ファイル システム リソースにデータを書き込む Stream インスタンスを返します。

GetRequestStream メソッドは、 Stream への同期アクセスを提供します。非同期アクセスでは、 BeginGetRequestStream メソッドと EndGetRequestStream メソッドを使用します。

使用例

[Visual Basic, C#, C++] GetRequestStream メソッドを使用して、ファイルに書き込むために使用するストリーム インスタンスを取得する例を次に示します。詳細については、 FileWebRequest クラスのトピックを参照してください。

 
' Enter the string to write to the file.
Console.WriteLine("Enter the string you want to write:")
Dim userInput As String = Console.ReadLine()

' Convert the string to a byte array.
Dim encoder As New ASCIIEncoding()
Dim byteArray As Byte() = encoder.GetBytes(userInput)

' Set the ContentLength property.
myFileWebRequest.ContentLength = byteArray.Length

Dim contentLength As String = myFileWebRequest.ContentLength.ToString()

Console.WriteLine(ControlChars.Lf + "The content length is {0}.", contentLength)


' Get the file stream handler to write to the file.
Dim readStream As Stream = myFileWebRequest.GetRequestStream()

' Write to the stream. 
' Note. For this to work the file must be accessible
' on the network. This can be accomplished by setting the property
' sharing of the folder containg the file.  
' FileWebRequest.Credentials property cannot be used for this purpose.
readStream.Write(byteArray, 0, userInput.Length)


Console.WriteLine(ControlChars.Lf + "The String you entered was successfully written to the file.")


[C#] 
// Enter the string to write to the file.
Console.WriteLine("Enter the string you want to write:");
string userInput = Console.ReadLine();
// Convert the string to a byte array.
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] byteArray = encoder.GetBytes(userInput);    
// Set the ContentLength property.
myFileWebRequest.ContentLength=byteArray.Length;  
string contentLength = myFileWebRequest.ContentLength.ToString();
Console.WriteLine("\nThe content length is {0}.", contentLength);  
// Get the file stream handler to write to the file.
Stream readStream=myFileWebRequest.GetRequestStream();      

// Write to the file stream. 
// Note.  For this to work, the file must be accessible
// on the network. This can be accomplished by setting the property
// sharing of the folder containg the file. 
// FileWebRequest.Credentials property cannot be used for this purpose.
readStream.Write(byteArray,0,userInput.Length);

Console.WriteLine("\nThe String you entered was successfully written to the file.");  

[C++] 
// Enter the string to write into the file.
Console::WriteLine(S"Enter the string you want to write:");
String* userInput = Console::ReadLine();
// Convert the string to Byte array.
ASCIIEncoding* encoder = new ASCIIEncoding();
Byte byteArray[] = encoder->GetBytes(userInput);
// Set the ContentLength property.
myFileWebRequest->ContentLength=byteArray->Length;
String* contentLength = myFileWebRequest->ContentLength.ToString();
Console::WriteLine(S"\nThe content length is {0}.", contentLength);
// Get the file stream handler to write into the file.
Stream* readStream=myFileWebRequest->GetRequestStream();

// Write to the file stream.
// Note. In order for this to work the file must be accessible
// on the network. This can be accomplished by setting the property
// sharing of the folder containg the file. The permissions
// can be set so everyone can modify the file.
// FileWebRequest::Credentials property cannot be used for this purpose.
readStream->Write(byteArray, 0, userInput->Length);

Console::WriteLine(S"\nThe String you entered was successfully written into the file.");

[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 ファミリ

参照

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