This documentation is archived and is not being maintained.

WebClient.OpenWrite Method (String, String)

.NET Framework 1.1

Opens a stream for writing data to the specified resource with using the specified method.

[Visual Basic]
Overloads Public Function OpenWrite( _
   ByVal address As String, _
   ByVal method As String _
) As Stream
public Stream OpenWrite(
 string address,
 string method
public: Stream* OpenWrite(
 String* address,
 String* method
public function OpenWrite(
   address : String,
 method : String
) : Stream;


The URI of the resource to receive the data.
The method used to send the data to the resource.

Return Value

A Stream used to write data to the resource.


Exception Type Condition
WebException The URI formed by combining BaseAddress, and address is invalid.


An error occurred while opening the stream.


The OpenWrite method returns a writable stream that is used to send data to a resource. The underlying request is made with the method specified in the method parameter.

If the method parameter specifies a method that is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the Status property set to indicate the error.

If the BaseAddress property is not empty, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not empty, it is appended to address.


[Visual Basic, C#, C++] The following example reads data from the command line and uses OpenWrite to obtain a stream used to write the data. Note that the Stream returned by OpenWrite is closed after the data is sent.

[Visual Basic] 
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.

Console.WriteLine(ControlChars.Cr + "Successfully 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");

// Close the stream and release resources.
Console.WriteLine("\nSuccessfully posted the data.");

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.
Console::WriteLine(S"\nSuccessfully posted the data.");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace | WebClient.OpenWrite Overload List