Export (0) Print
Expand All

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
[C#]
public Stream OpenWrite(
 string address,
 string method
);
[C++]
public: Stream* OpenWrite(
 String* address,
 String* method
);
[JScript]
public function OpenWrite(
   address : String,
 method : String
) : Stream;

Parameters

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

Return Value

A Stream used to write data to the resource.

Exceptions

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

-or-

An error occurred while opening the stream.

Remarks

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.

Example

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

Requirements

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

Show:
© 2014 Microsoft