Export (0) Print
Expand All

FileWebRequest.BeginGetRequestStream Method

Begins an asynchronous request for a Stream object to use to write data.

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

'Declaration
<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading := True)> _
Public Overrides Function BeginGetRequestStream ( _
	callback As AsyncCallback, _
	state As Object _
) As IAsyncResult

Parameters

callback
Type: System.AsyncCallback

The AsyncCallback delegate.

state
Type: System.Object

An object that contains state information for this request.

Return Value

Type: System.IAsyncResult
An IAsyncResult that references the asynchronous request.

ExceptionCondition
ProtocolViolationException

The Method property is GET and the application writes to the stream.

InvalidOperationException

The stream is being used by a previous call to BeginGetRequestStream.

ApplicationException

No write stream is available.

WebException

The FileWebRequest was aborted.

The BeginGetRequestStream method starts an asynchronous request for a stream used to send data to a file system resource. The callback method that implements the AsyncCallback delegate uses the EndGetRequestStream method to return the request stream.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example uses BeginGetRequestStream to make an asynchronous request for a Stream object.

Public Class RequestDeclare
    Public myFileWebRequest As FileWebRequest
    Public userinput As [String]


    Public Sub New()
        myFileWebRequest = Nothing 
    End Sub ' New 
End Class ' RequestDeclare


Class FileWebRequest_reqbeginend
    Public Shared allDone As New ManualResetEvent(False)

    ' Entry point which delegates to C-style main Private Function. 
    Public Overloads Shared Sub Main()
        Main(System.Environment.GetCommandLineArgs())
    End Sub 


    Overloads Shared Sub Main(args() As String)
        If args.Length < 2 Then
            Console.WriteLine(ControlChars.Cr + "Please enter the file name as command line parameter:")
            Console.WriteLine("Usage:FileWebRequest_reqbeginend <systemname>/<sharedfoldername>/<filename>")
	    Console.WriteLine("Example: FileWebRequest_reqbeginend shafeeque/shaf/hello.txt")

        Else 
            Try 

                ' Place a webrequest. 
                Dim myWebRequest As WebRequest = WebRequest.Create(("file://" + args(1)))

                ' Create an instance of the 'RequestDeclare' and associate the 'myWebRequest' to it.		 
                Dim requestDeclare As New RequestDeclare()
                requestDeclare.myFileWebRequest = CType(myWebRequest, FileWebRequest)
                ' Set the 'Method' property of 'FileWebRequest' object to 'POST' method.
                requestDeclare.myFileWebRequest.Method = "POST"
                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 . 
                Dim r As IAsyncResult = CType(requestDeclare.myFileWebRequest.BeginGetRequestStream(AddressOf ReadCallback, requestDeclare), IAsyncResult)
                allDone.WaitOne()

                Console.Read()
            Catch e As ProtocolViolationException
                Console.WriteLine(("ProtocolViolationException is :" + e.Message))
            Catch e As InvalidOperationException
                Console.WriteLine(("InvalidOperationException is :" + e.Message))
            Catch e As UriFormatException
                Console.WriteLine(("UriFormatExceptionException is :" + e.Message))
            End Try 

        End If  
    End Sub 'Main


    Private Shared Sub ReadCallback(ar As IAsyncResult)
        Try 

            ' State of the request is asynchronous. 
            Dim requestDeclare As RequestDeclare = CType(ar.AsyncState, RequestDeclare)
            Dim myFileWebRequest As FileWebRequest = requestDeclare.myFileWebRequest
            Dim sendToFile As [String] = requestDeclare.userinput

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

            ' Convert the string into byte array. 
            Dim encoder As New ASCIIEncoding()
            Dim byteArray As Byte() = encoder.GetBytes(sendToFile)

            ' Write to the stream.
            readStream.Write(byteArray, 0, sendToFile.Length)
            readStream.Close()
            allDone.Set()


            Console.WriteLine(ControlChars.Cr +"The String you entered was successfully written into the file.")
            Console.WriteLine(ControlChars.Cr +"Press Enter to continue.")

            Catch e As ApplicationException
            Console.WriteLine(("ApplicationException is :" + e.Message))
        End Try 

    End Sub ' ReadCallback 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft