WebRequest GetResponseStream 读取字节

发布于 2024-12-20 21:36:44 字数 1004 浏览 0 评论 0原文

我正在尝试从 ResponeStream 读取字节,但我怎么能说要等待数据呢?

如果我在 GetResponseStream 之后设置断点并等待几秒钟,一切正常。 使用 StreamReader.ReadToEnd() 也可以正常工作,但我想自己读取字节。

byte[] response = null;

int left = 0;
int steps = 0;
int pos = 0;

int bytelength = 1024;

OnReceiveStart();

using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) {
    using (Stream sr = webResponse.GetResponseStream()) {

        response = new byte[(int)webResponse.ContentLength];

        left = (int)webResponse.ContentLength % bytelength;
        steps = (int)webResponse.ContentLength / bytelength;
        pos = 0;

        for (int i = 0; i < steps; i++) {
            sr.Read(response, pos, bytelength);
            pos += bytelength;
            OnReceiveProgress((int)webResponse.ContentLength, pos);
        }

        if (left != 0) {
            sr.Read(response, pos, left);
        }

        sr.Close();
    }
    webResponse.Close();
}

OnReceiveProgress(1, 1);

OnReceiveFinished();

I'm trying to read bytes from an ResponeStream, but how can i say to wait for the data?

If i set a breakpoint after GetResponseStream and wait a few seconds all works fine.
Using StreamReader.ReadToEnd() also works fine, but i want to read the bytes myself.

byte[] response = null;

int left = 0;
int steps = 0;
int pos = 0;

int bytelength = 1024;

OnReceiveStart();

using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) {
    using (Stream sr = webResponse.GetResponseStream()) {

        response = new byte[(int)webResponse.ContentLength];

        left = (int)webResponse.ContentLength % bytelength;
        steps = (int)webResponse.ContentLength / bytelength;
        pos = 0;

        for (int i = 0; i < steps; i++) {
            sr.Read(response, pos, bytelength);
            pos += bytelength;
            OnReceiveProgress((int)webResponse.ContentLength, pos);
        }

        if (left != 0) {
            sr.Read(response, pos, left);
        }

        sr.Close();
    }
    webResponse.Close();
}

OnReceiveProgress(1, 1);

OnReceiveFinished();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-12-27 21:36:44

只是不要将其分解为相同数量的步骤 - 相反,只需继续循环读取直到完成:

while (pos < response.Length)
{
    int bytesRead = sr.Read(response, pos, response.Length - pos);
    if (bytesRead == 0)
    {
        // End of data and we didn't finish reading. Oops.
        throw new IOException("Premature end of data");
    }
    pos += bytesRead;
    OnReceiveProgress(response.Length, pos);
}

请注意,您必须使用 Stream.Read< 的返回值/code> - 你不能假设它会读取你所要求的所有内容。

Just don't break it into your equal number of steps - instead, just keep reading in a loop until you're done:

while (pos < response.Length)
{
    int bytesRead = sr.Read(response, pos, response.Length - pos);
    if (bytesRead == 0)
    {
        // End of data and we didn't finish reading. Oops.
        throw new IOException("Premature end of data");
    }
    pos += bytesRead;
    OnReceiveProgress(response.Length, pos);
}

Note that you must use the return value of Stream.Read - you can't assume it will read everything you've asked for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文