Export (0) Print
Expand All

WebClient.OpenWrite Method

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

Overload List

Opens a stream for writing data to the specified resource.

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

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

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

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, C#, C++] Note   This example shows how to use one of the overloaded versions of OpenWrite. For other examples that might be available, see the individual overload topics.
[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.

See Also

WebClient Class | WebClient Members | System.Net Namespace

Show:
© 2014 Microsoft