0
嘗試從異步webrequest進行readToEnd時遇到了一些問題。下面的代碼:異步I/O(從異步webrequest讀取流)
public void GetHTTP(string http)
{
request = (HttpWebRequest)WebRequest.Create(http);
RequestState rs = new RequestState(); // Class to hold state. Can ignore...
Setup(); // contain statements such as request.Accept =...;
rs.Request = request;
IAsyncResult r = (IAsyncResult)request.BeginGetResponse(new AsyncCallback ResponseSetup), rs);
allDone.WaitOne();
Referer = http; //Can ignore this...
}
private void ResponseSetup(IAsyncResult ar)
{
RequestState rs = (RequestState)ar.AsyncState;
WebRequest request = rs.Request;
WebResponse response = request.EndGetResponse(ar);
Stream ResponseStream = response.GetResponseStream();
rs.ResponseStream = ResponseStream;
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
StreamReader reader = new StreamReader(ResponseStream);
information = reader.ReadToEnd();
//The problem is right here; ReadToEnd.
}
當試圖調用ReadToEnd的方法,我得到這個錯誤信息:該流不支持併發I/O讀取或寫入操作。
我查過了,但找不到解決這個問題的辦法。它如何被修復?
'ReadToEnd'是一種阻塞方法,您可以調用*代替使用異步方法。在異步'ReadCallBack'中,你將不得不使用提供給緩衝區的數據自己讀取數據。 – 2010-11-06 18:49:56