异步I/O(从异步Webrequest读取流) [英] Asynchronous I/O (Reading stream from an asynchronous webrequest)

查看:83
本文介绍了异步I/O(从异步Webrequest读取流)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从异步webrequest读取readToEnd时,我遇到了一些问题.这是代码:

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.
    }

当尝试调用readToEnd方法时,出现以下错误消息:流不支持并发I/O读取或写入操作.

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?

推荐答案

那是因为您要进行两次读取.调用BeginRead会启动读取操作.然后ReadToEnd在同一流上启动另一个读取操作.

That's because you're trying to do two reads. The call to BeginRead initiates a read operation. Then ReadToEnd initiates another read operation on the same stream.

我想您想要的只是ReadToEnd.删除对ResponseStream.BeginRead的呼叫.

I think what you want is just the ReadToEnd. Remove the call to ResponseStream.BeginRead.

这篇关于异步I/O(从异步Webrequest读取流)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆