This documentation is archived and is not being maintained.

FileWebRequest.GetRequestStream Method

Returns a Stream object for writing data to the file system resource.

Namespace: System.Net
Assembly: System (in system.dll)

'Declaration
Public Overrides Function GetRequestStream As Stream
'Usage
Dim instance As FileWebRequest
Dim returnValue As Stream

returnValue = instance.GetRequestStream
public Stream GetRequestStream ()
public override function GetRequestStream () : Stream

Return Value

A Stream for writing data to the file system resource.

Exception typeCondition

WebException

The request times out.

The GetRequestStream method provides synchronous access to the Stream. For asynchronous access, use the BeginGetRequestStream and EndGetRequestStream methods.

The following code example uses the GetRequestStream method to obtain a stream instance used to write to the file. Refer to the complete example in the FileWebRequest class.

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


// 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();
ubyte byteArray[] = encoder.GetBytes(userInput);

// Set the ContentLength property.
myFileWebRequest.set_ContentLength(byteArray.length);
String contentLength = 
    (new Long(myFileWebRequest.get_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.get_Length());
Console.WriteLine("\nThe String you entered was successfully " 
    + "written to the file.");

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: