第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

分塊流響應(yīng)不正確

分塊流響應(yīng)不正確

C#
眼眸繁星 2022-01-09 16:26:31
抱歉,標(biāo)題這么模糊,我真的不知道該問題的標(biāo)題是什么?;旧希?dāng)我得到傳輸編碼所告訴的分塊流時,我然后執(zhí)行以下代碼:private IEnumerable<byte[]> ReceiveMessageBodyChunked() {    readChunk:    #region Read a line from the Stream which should be a Block Length (Chunk Body Length)    string blockLength = _receiverHelper.ReadLine();    #endregion    #region If the end of the block is reached, re-read from the stream    if (blockLength == Http.NewLine) {        goto readChunk;    }    #endregion    #region Trim it so it should end up with JUST the number    blockLength = blockLength.Trim(' ', '\r', '\n');    #endregion    #region If the end of the message body is reached    if (blockLength == string.Empty) {        yield break;    }    #endregion    int blockLengthInt = 0;    #region Convert the Block Length String to an Int32 base16 (hex)    try {        blockLengthInt = Convert.ToInt32(blockLength, 16);    } catch (Exception ex) {        if (ex is FormatException || ex is OverflowException) {            throw new Exception(string.Format(ExceptionValues.HttpException_WrongChunkedBlockLength, blockLength), ex);        }        throw;    }    #endregion    // If the end of the message body is reached.    if (blockLengthInt == 0) {        yield break;    }    byte[] buffer = new byte[blockLengthInt];    int totalBytesRead = 0;    while (totalBytesRead != blockLengthInt) {        int length = blockLengthInt - totalBytesRead;        if (bytesRead == 0) {            WaitData();            continue;        }        totalBytesRead += bytesRead;        yield return buffer;    }    goto readChunk;}這樣做是從應(yīng)該是塊長度的流中讀取 1 行數(shù)據(jù),在這里和那里進(jìn)行一些檢查,但最終將其轉(zhuǎn)換為 Int32 Radix16 整數(shù)。從那里它基本上創(chuàng)建了一個 int32 的字節(jié)緩沖區(qū)作為它的長度大小。然后它只是繼續(xù)從流中讀取,直到它讀取與我們轉(zhuǎn)換的 Int32 相同的數(shù)量。這很好用,但是,無論出于何種原因,它在最后一次閱讀時響應(yīng)不正確。它將讀取確切的字節(jié)數(shù),因?yàn)閴K長度非常好,并且我期望的所有數(shù)據(jù)都被讀取。但它也在再次讀取另一小塊數(shù)據(jù),這些數(shù)據(jù)在最后已經(jīng)讀取,導(dǎo)致我們可以說從ASWELL<!DOCTYPE html>到</html>ASWELL 的所有數(shù)據(jù)作為來自內(nèi)部某處的一些數(shù)據(jù)<form>等這是發(fā)生的事情的一個例子:如您所見,突出顯示的紅色文本不應(yīng)該從閱讀中返回!它應(yīng)該在 結(jié)束</html>。為什么 Chunk's Length 對我說謊,我怎樣才能找到合適的尺寸來閱讀?
查看完整描述

1 回答

?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個贊

我不熟悉 C# 但如果我正確理解了您的代碼和ReadC# 中的語義(這似乎與readC 中的相似),那么問題是您一次又一次地使用相同的緩沖區(qū)而沒有先重置它:


byte[] buffer = new byte[blockLengthInt];

int totalBytesRead = 0;

while (totalBytesRead != blockLengthInt) {

    int length = blockLengthInt - totalBytesRead;

    int bytesRead = _receiverHelper.HasData ? _receiverHelper.Read(buffer, 0, length) : _request.ClientStream.Read(buffer, 0, length);

    ...

    totalBytesRead += bytesRead;

    ...

    yield return buffer;

}

舉例說明這里出了什么問題:假設(shè)塊大小為 10,您讀取的內(nèi)容是0123456789,第一次讀取將返回 6 個字節(jié),第二次讀取剩余的 4 個字節(jié)。在這種情況下,您的緩沖區(qū)將012345在第一次讀取567845之后和第二次讀取之后。這些45在緩沖區(qū)末尾量與上一讀,因?yàn)槟阒皇侨〈司彌_區(qū)中的第4個字節(jié),但保留了休息。


奇怪的是,如果我將請求交給另一個 TCPStream 代理(127.0.0.1:8888 作為代理,它是提琴手),它工作得非常好......


Fiddler 是一個代理,可能會改變響應(yīng)的傳輸方式。例如,它可能使用Content-length分塊編碼而不是分塊編碼,或者它可能使用較小的塊,以便您始終在第一次讀取時獲得完整的塊。


查看完整回答
反對 回復(fù) 2022-01-09
  • 1 回答
  • 0 關(guān)注
  • 174 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號