本文整理匯總了VB.NET中System.Net.FileWebRequest.BeginGetRequestStream方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET FileWebRequest.BeginGetRequestStream方法的具體用法?VB.NET FileWebRequest.BeginGetRequestStream怎麽用?VB.NET FileWebRequest.BeginGetRequestStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Net.FileWebRequest
的用法示例。
在下文中一共展示了FileWebRequest.BeginGetRequestStream方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: RequestDeclare
Public Class RequestDeclare
Public myFileWebRequest As FileWebRequest
Public userinput As [String]
Public Sub New()
myFileWebRequest = Nothing
End Sub
End Class
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
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