HttpWebRequest.BeginGetRequestStream 方法

开始对用来写入数据的 Stream 对象的异步请求。

**命名空间:**System.Net
**程序集:**System(在 system.dll 中)

语法

声明
Public Overrides Function BeginGetRequestStream ( _
    callback As AsyncCallback, _
    state As Object _
) As IAsyncResult
用法
Dim instance As HttpWebRequest
Dim callback As AsyncCallback
Dim state As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginGetRequestStream(callback, state)
public override IAsyncResult BeginGetRequestStream (
    AsyncCallback callback,
    Object state
)
public:
virtual IAsyncResult^ BeginGetRequestStream (
    AsyncCallback^ callback, 
    Object^ state
) override
public IAsyncResult BeginGetRequestStream (
    AsyncCallback callback, 
    Object state
)
public override function BeginGetRequestStream (
    callback : AsyncCallback, 
    state : Object
) : IAsyncResult

参数

  • state
    此请求的状态对象。

返回值

引用该异步请求的 IAsyncResult

异常

异常类型 条件

ProtocolViolationException

Method 属性为 GET 或 HEAD。

- 或 -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。

InvalidOperationException

流正由上一个 BeginGetRequestStream 调用使用。

- 或 -

TransferEncoding 被设置为一个值,并且 SendChunkedfalse

- 或 -

线程池中的线程即将用完。

NotSupportedException

请求缓存验证程序指示对此请求的响应可从缓存中提供;但是写入数据的请求不得使用缓存。如果您正在使用错误实现的自定义缓存验证程序,则会发生此异常。

WebException

Abort 以前被调用过。

ObjectDisposedException

在 .NET Compact Framework 应用程序中,未正确获得和关闭一个内容长度为零的请求流。有关处理内容长度为零的请求的更多信息,请参见 .NET Compact Framework 中的网络编程

备注

BeginGetRequestStream 方法开始对用于发送 HttpWebRequest 数据的流的异步请求。异步回调方法使用 EndGetRequestStream 方法返回实际的流。

要了解有关线程池的更多信息,请参见 托管线程池

提示

应用程序不能对特定请求混合使用同步和异步方法。如果调用 BeginGetRequestStream 方法,则必须使用 BeginGetResponse 方法检索响应。

提示

当在应用程序中启用网络跟踪功能后,此成员将输出跟踪信息。有关更多信息,请参见 网络跟踪

示例

下面的代码示例使用 BeginGetRequestStream 方法对流实例发出异步请求。

Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Threading
Imports Microsoft.VisualBasic

Class HttpWebRequestBeginGetRequest
    Public Shared allDone As New ManualResetEvent(False)

    Shared Sub Main()


        ' Create a new HttpWebRequest object.
      '  Dim request As HttpWebRequest = CType(WebRequest.Create("https://www.contoso.com/example.aspx"), _
       '         HttpWebRequest)
Dim request As HttpWebRequest = CType(WebRequest.Create("https://localhost/test/PostAccepter.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 ReadCallback, 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()
        '  Get the response.
        Dim response As HttpWebResponse = CType(request.GetResponse(), 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.
        response.Close()
            
    End Sub ' Main

    Private Shared Sub ReadCallback(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()
        allDone.Set()
    End Sub ' ReadCallback

End Class ' HttpWebRequest_BeginGetRequest
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;

class HttpWebRequestBeginGetRequest
{
    public static ManualResetEvent allDone = new ManualResetEvent(false);
    public static void Main()
    {
        

            // Create a new HttpWebRequest object.
            HttpWebRequest request=(HttpWebRequest) WebRequest.Create("https://www.contoso.com/example.aspx");    
    
            // 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.    
            request.BeginGetRequestStream(new AsyncCallback(ReadCallback), 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();

            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            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();
        }
    
    private static void ReadCallback(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 ();
            allDone.Set();    
    }

}
#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::IO;
using namespace System::Text;
using namespace System::Threading;
ref class HttpWebRequestBeginGetRequest
{
public:
   static ManualResetEvent^ allDone = gcnew ManualResetEvent( false );
   static void Main()
   {
      
      // Create a new HttpWebRequest object.
      HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "https://www.contoso.com/example.aspx" ));
      
      // 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.    
      AsyncCallback^ del = gcnew AsyncCallback( ReadCallback );
      request->BeginGetRequestStream( del, 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();
      HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
      Stream^ streamResponse = response->GetResponseStream();
      StreamReader^ streamRead = gcnew StreamReader( streamResponse );
      String^ responseString = streamRead->ReadToEnd();
      Console::WriteLine( responseString );
      
      // Close Stream object.
      streamResponse->Close();
      streamRead->Close();
      
      // Release the HttpWebResponse.
      response->Close();
   }


private:
   static void ReadCallback( IAsyncResult^ asynchronousResult )
   {
      HttpWebRequest^ request = dynamic_cast<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 Byte array.
      array<Byte>^ByteArray = Encoding::UTF8->GetBytes( postData );
      
      // Write to the request stream.
      postStream->Write( ByteArray, 0, postData->Length );
      postStream->Close();
      allDone->Set();
   }

};

void main()
{
   HttpWebRequestBeginGetRequest::Main();
}
import System.*;
import System.Net.*;
import System.IO.*;
import System.Text.*;
import System.Threading.*;

class HttpWebRequestBeginGetRequest
{
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    public static void main(String[] args)
    {
        // Create a new HttpWebRequest object.
        HttpWebRequest request = (HttpWebRequest)(WebRequest.Create(
            "https://www.contoso.com/example.aspx"));
        // Set the ContentType property. 
        request.set_ContentType("application/x-www-form-urlencoded");
        // Set the Method property to 'POST' to post data to the URI.
        request.set_Method("POST");
        // Start the asynchronous operation.    
        request.BeginGetRequestStream(new AsyncCallback(ReadCallback), 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();

        // Get the response.
        HttpWebResponse response = (HttpWebResponse)(request.GetResponse());
        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();
    } //main

    private static void ReadCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)
            (asynchronousResult.get_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.
        ubyte byteArray[] = Encoding.get_UTF8().GetBytes(postData);
        // Write to the request stream.
        postStream.Write(byteArray, 0, postData.get_Length());
        postStream.Close();
        allDone.Set();
    } //ReadCallback
} //HttpWebRequestBeginGetRequest 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

HttpWebRequest 类
HttpWebRequest 成员
System.Net 命名空间

其他资源

defaultProxy 元素(网络设置)