本文整理匯總了VB.NET中System.Net.HttpWebRequest.BeginGetRequestStream方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET HttpWebRequest.BeginGetRequestStream方法的具體用法?VB.NET HttpWebRequest.BeginGetRequestStream怎麽用?VB.NET HttpWebRequest.BeginGetRequestStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了HttpWebRequest.BeginGetRequestStream方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: HttpWebRequestBeginGetRequest
' 導入命名空間
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Threading
Class HttpWebRequestBeginGetRequest
Public Shared allDone As New ManualResetEvent(False)
Shared Sub Main()
' Create a new HttpWebRequest object.
Dim request As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com/example.aspx"), _
HttpWebRequest)
' Set the ContentType property.
request.ContentType = "application/x-www-form-urlencoded"
' Set the Method property to 'POST' to post data to the URI.
request.Method = "POST"
' Start the asynchronous operation.
Dim result As IAsyncResult = _
CType(request.BeginGetRequestStream(AddressOf GetRequestStreamCallback, request), _
IAsyncResult)
' Keep the main thread from continuing while the asynchronous
' operation completes. A real world application
' could do something useful such as updating its user interface.
allDone.WaitOne()
End Sub
Private Shared Sub GetRequestStreamCallback(ByVal asynchronousResult As IAsyncResult)
Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
' End the operation
Dim postStream As Stream = request.EndGetRequestStream(asynchronousResult)
Console.WriteLine("Please enter the input data to be posted:")
Dim postData As [String] = Console.ReadLine()
' Convert the string into byte array.
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Write to the stream.
postStream.Write(byteArray, 0, postData.Length)
postStream.Close()
' Start the asynchronous operation to get the response
Dim result As IAsyncResult = _
CType(request.BeginGetResponse(AddressOf GetResponseCallback, request), _
IAsyncResult)
End Sub
Private Shared Sub GetResponseCallback(ByVal asynchronousResult As IAsyncResult)
Dim request As HttpWebRequest = CType(asynchronousResult.AsyncState, HttpWebRequest)
' Get the response.
Dim response As HttpWebResponse = CType(request.EndGetResponse(asynchronousResult), _
HttpWebResponse)
Dim streamResponse As Stream = response.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim responseString As String = streamRead.ReadToEnd()
Console.WriteLine(responseString)
' Close Stream object.
streamResponse.Close()
streamRead.Close()
' Release the HttpWebResponse.
allDone.Set()
response.Close()
End Sub
End Class