HttpWebRequest.BeginGetRequestStream(AsyncCallback, Object) 方法      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
开始 Stream 对象用于写入数据的异步请求。
public:
 override IAsyncResult ^ BeginGetRequestStream(AsyncCallback ^ callback, System::Object ^ state);public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);public override IAsyncResult BeginGetRequestStream(AsyncCallback? callback, object? state);override this.BeginGetRequestStream : AsyncCallback * obj -> IAsyncResultPublic Overrides Function BeginGetRequestStream (callback As AsyncCallback, state As Object) As IAsyncResult参数
- callback
- AsyncCallback
AsyncCallback 委托。
- state
- Object
此请求的状态对象。
返回
引用异步请求的 IAsyncResult。
例外
Method 属性为 GET 或 HEAD。
-或-
              KeepAlive
              true,AllowWriteStreamBuffering 为 false,ContentLength 为 -1,SendChunked 为 false,Method 为 POST 或 PUT。
上一次调用 BeginGetRequestStream(AsyncCallback, Object) 正在使用流
-或-
              TransferEncoding 设置为值,SendChunkedfalse。
-或-
线程池已耗尽线程。
请求缓存验证程序指示可从缓存中提供此请求的响应;但是,写入数据的请求不得使用缓存。 如果使用未正确实现的自定义缓存验证程序,则可能会出现此异常。
以前调用 Abort()。
在 .NET Compact Framework 应用程序中,未正确获取和关闭长度为零的请求流。 有关处理零内容长度请求的详细信息,请参阅 .NET Compact Framework中的 
示例
下面的代码示例使用 BeginGetRequestStream 方法为流实例发出异步请求。
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
class HttpWebRequestBeginGetRequest
{
    private static ManualResetEvent allDone = new ManualResetEvent(false);
    public static void Main(string[] args)
    {
        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");
        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
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        // 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();
    }
    private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the operation
        Stream postStream = request.EndGetRequestStream(asynchronousResult);
        Console.WriteLine("Please enter the input data to be posted:");
        string postData = Console.ReadLine();
        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.Length);
        postStream.Close();
        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }
    private static void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        // End the operation
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        Console.WriteLine(responseString);
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();
        // Release the HttpWebResponse
        response.Close();
        allDone.Set();
    }
}
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
注解
谨慎
              WebRequest、HttpWebRequest、ServicePoint和 WebClient 已过时,不应将其用于新开发。 请改用 HttpClient。
BeginGetRequestStream 方法启动用于发送 HttpWebRequest数据的流的异步请求。 异步回调方法使用 EndGetRequestStream 方法返回实际流。
BeginGetRequestStream 方法要求完成一些同步安装任务(例如 DNS 解析、代理检测和 TCP 套接字连接),然后此方法才变为异步。 因此,不应在用户界面(UI)线程上调用此方法,因为它可能需要相当长的时间(最多几分钟,具体取决于网络设置)才能完成初始同步设置任务,然后引发错误异常或方法成功。
若要详细了解线程池,请参阅 托管线程池。
注意
应用程序无法混合特定请求的同步和异步方法。 如果调用 BeginGetRequestStream 方法,则必须使用 BeginGetResponse 方法来检索响应。
注意
在应用程序中启用网络跟踪时,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework中的