FileWebRequest.EndGetRequestStream Method (IAsyncResult)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Ends an asynchronous request for a Stream instance that the application uses to write data.

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

public override Stream EndGetRequestStream(
	IAsyncResult asyncResult


Type: System.IAsyncResult

An IAsyncResult that references the pending request for a stream.

Return Value

Type: System.IO.Stream

A Stream object that the application uses to write data.

Exception Condition

asyncResult is null.

The EndGetRequestStream method completes an asynchronous stream request that was started by the BeginGetRequestStream method.


To avoid timing issues with garbage collection, be sure to close the response stream by calling the Close method on the stream returned by the GetResponseStream method after calling the EndGetResponse method.

The following example uses the EndGetRequestStream method to end the asynchronous request for a Stream object.

public class RequestDeclare
	 public FileWebRequest myFileWebRequest;	
	 public String userinput;

		public RequestDeclare()
		myFileWebRequest = null;

class FileWebRequest_reqbeginend
	public static ManualResetEvent allDone = new ManualResetEvent(false);

	static void Main(string[] args)
	  if (args.Length < 1)
  		    Console.WriteLine("\nPlease enter the file name as command line parameter:");
		       Console.WriteLine("Usage:FileWebRequest_reqbeginend <systemname>/<sharedfoldername>/<filename>\nExample:FileWebRequest_reqbeginend shafeeque/shaf/hello.txt");



		      // Place a webrequest.
		      WebRequest myWebRequest= WebRequest.Create("file://"+args[0]);

    	      // Create an instance of the 'RequestDeclare' and associate the 'myWebRequest' to it.		
		      RequestDeclare requestDeclare = new RequestDeclare();
		      requestDeclare.myFileWebRequest = (FileWebRequest)myWebRequest;
		      // Set the 'Method' property of 'FileWebRequest' object to 'POST' method.
		      Console.WriteLine("Enter the string you want to write into the file:");
		      requestDeclare.userinput = Console.ReadLine();

		      // Begin the Asynchronous request for getting file content using 'BeginGetRequestStream()' method .
		      IAsyncResult r=(IAsyncResult) requestDeclare.myFileWebRequest.BeginGetRequestStream(new AsyncCallback(ReadCallback),requestDeclare);			

	    catch(ProtocolViolationException e)
	          Console.WriteLine("ProtocolViolationException is :"+e.Message);
	    catch(InvalidOperationException e)
		   	Console.WriteLine("InvalidOperationException is :"+e.Message);
	    catch(UriFormatException e)
			Console.WriteLine("UriFormatExceptionException is :"+e.Message);

	private static void ReadCallback(IAsyncResult ar)


		  // State of the request is asynchronous.
		  RequestDeclare requestDeclare=(RequestDeclare) ar.AsyncState;
		  FileWebRequest myFileWebRequest=requestDeclare.myFileWebRequest;
		  String sendToFile = requestDeclare.userinput;

		  // End the Asynchronus request by calling the 'EndGetRequestStream()' method.
		  Stream readStream=myFileWebRequest.EndGetRequestStream(ar);

		  // Convert the string into byte array.

		  ASCIIEncoding encoder = new ASCIIEncoding();
		  byte[] byteArray = encoder.GetBytes(sendToFile);

		  // Write to the stream.

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

       catch(ApplicationException e)
		  Console.WriteLine("ApplicationException is :"+e.Message);


.NET Framework
Available since 1.1
Return to top