2013-12-15 65 views
2

我開發一個HTTP服務器, 我的響應代碼總結是這樣的:塊傳輸編碼

PHTTP_DATA_CHUNK p = new HTTP_DATA_CHUNK[count]; 
for (int i = 0; i<count; i++) 
{ 
    p[i].DataChunkType = HttpDataChunkFromMemory; 
    p[i].FromMemory.pBuffer = "dfdff"; 
    p[i].FromMemory.BufferLength = 5; 
} 

HTTP_RESPONSE response; 
ZeroMemory(&response,sizeof(HTTP_RESPONSE)); 
PCSTR Reason="OK"; 
response.StatusCode=200; 
response.pReason=Reason; 
response.ReasonLength=strlen(Reason); 
ADD_KNOWN_HEADER(response, HttpHeaderContentType, "text/html"); 
ADD_KNOWN_HEADER(response, HttpHeaderConnection, "keep-alive"); 
ADD_KNOWN_HEADER(response, HttpHeaderTransferEncoding, "chunked"); 
ADD_KNOWN_HEADER(response, HttpHeaderContentLength, chLen); 

response.EntityChunkCount = count; 
response.pEntityChunks=p; 
ULONG BytesSent; 
ULONG result = HttpSendHttpResponse(ReqQueueHandle, HttpRequest->RequestId, 
              0, &response, NULL,&BytesSent, NULL, 
              0,NULL,NULL, NULL); 

但結果是87! 現在,如果我刪除此行的代碼:

ADD_KNOWN_HEADER(response, HttpHeaderTransferEncoding, "chunked"); 

結果是0,我的響應發送給客戶端。 如何使用Chunked transfer encoding

+0

錯誤代碼:'87'是'ERROR_INVALID_PARAMETER' – Sebastian

+0

我知道這一點。謝謝 – nabegheh95

回答

1

當我重新排列以下列方式代碼,反應正確發送:

response.EntityChunkCount = 0; 
response.pEntityChunks=0; 
ULONG BytesSent; 
ULONG result = HttpSendHttpResponse(ReqQueueHandle, HttpRequest->requestId, 
              0, &response, NULL,&BytesSent, NULL, 
              0,NULL,NULL, NULL); 
for (/*buffers */) 
{ 
    PHTTP_DATA_CHUNK chunk; 
    chunk.DataChunkType = HttpDataChunkFromMemory; 
    chunk.FromMemory.pBuffer = buffer[i]; //--- "bufferLen\r\n .... \r\n" 
    chunk.FromMemory.BufferLength = len[i]; 
    HttpSendResponseEntityBody(ReqQueueHandle, HttpRequest->requestId, 
           HTT_SEND_RESPONSE_FLAG_MORE_DATA, 1, &chunk, 0 
           NULL, 0, NULL, NULL); 
} 

PHTTP_DATA_CHUNK chunkEnd; 
chunkEnd.DataChunkType = HttpDataChunkFromMemory; 
chunkEnd.FromMemory.pBuffer = "\r\n0\r\n"; 
chunkEnd.FromMemory.BufferLength = 5; 
HttpSendResponseEntityBody(ReqQueueHandle, HttpRequest->requestId, 
           HTT_SEND_RESPONSE_FLAG_DISCONNECT, 1, &chunkEnd, 0 
           NULL, 0, NULL, NULL);