I’m running into a bit of problem when trying to readToEnd from an asynchronous webrequest. Here’s the code:
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.
}
When trying to invoke the readToEnd method, I get this error message: The stream does not support concurrent I/O read or write operations.
I’ve searched, but I could not find a solution to this problem. How can it be fixed?
That’s because you’re trying to do two reads. The call to
BeginReadinitiates a read operation. ThenReadToEndinitiates another read operation on the same stream.I think what you want is just the
ReadToEnd. Remove the call toResponseStream.BeginRead.