本文整理汇总了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