本文档已存档,并且将不进行维护。

HttpWebRequest.BeginGetRequestStream 方法

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

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

public override IAsyncResult BeginGetRequestStream (
	AsyncCallback callback,
	Object state
)
public IAsyncResult BeginGetRequestStream (
	AsyncCallback callback, 
	Object state
)
public override function BeginGetRequestStream (
	callback : AsyncCallback, 
	state : Object
) : IAsyncResult

参数

callback

AsyncCallback 委托。

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 方法返回实际的流。

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

Note注意

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

Note注意

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

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

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("http://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();    
    }

}

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(
            "http://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
显示: