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 -> IAsyncResult
Public Overrides Function BeginGetRequestStream (callback As AsyncCallback, state As Object) As IAsyncResult
参数
- callback
- AsyncCallback
委托 AsyncCallback 。
- state
- Object
此请求的状态对象。
返回
引用异步请求的一个 IAsyncResult 。
例外
该 Method 属性为 GET 或 HEAD。
-或-
KeepAliveistrue、isAllowWriteStreamBuffering、 falseContentLength is -1、 SendChunked is false和 Method IS POST 或 PUT。
上一次调用正在使用流 BeginGetRequestStream(AsyncCallback, Object)
-或-
TransferEncoding 设置为一个值,并且 SendChunked 是 false。
-或-
线程池已耗尽线程。
请求缓存验证程序指示可从缓存中提供此请求的响应;但是,写入数据的请求不得使用缓存。 如果使用未正确实现的自定义缓存验证程序,则可能会出现此异常。
Abort() 以前被调用过。
在 .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 中的 Network Tracing。