我正在制作异步请求BeginGetRequestStream
,其中回调处理EndRequest
。我想将回调方法ReadCallback()
中发生的任何异常发送回主调用方法InsertSoapEnvelopeIntoWebRequest
。
private void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml,
HttpWebRequest webRequest)
{
try
{
webRequest.ContentLength = soapEnvelopeXml.InnerXml.Length;
this.soapEnvelopeXml = soapEnvelopeXml;
allDone = new ManualResetEvent(false);
IAsyncResult r = (IAsyncResult)webRequest.BeginGetRequestStream(
new AsyncCallback(ReadCallback), webRequest);
allDone.WaitOne();
}
catch(Exception ex)
{
throw;
}
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest myRequestState = (HttpWebRequest)asynchronousResult.AsyncState;
byte[] byteArray = null;
using (Stream streamResponse =
myRequestState.EndGetRequestStream(asynchronousResult))
{
byteArray = Encoding.UTF8.GetBytes(soapEnvelopeXml.InnerXml);
streamResponse.Write(byteArray, 0, byteArray.Length);
}
allDone.Set();
}
我如何实现这一目标?
谢谢, GAGAN