本文介绍了分块传输编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! hi i开发一个Http服务器, 我的响应代码摘要是: PHTTP_DATA_CHUND p = new HTTP_DATA_CHUND [count]; for(int i = 0; i< count; i ++) { p [i] .DataChunkType = HttpDataChunkFromMemory; p [i] .FromMemory.pBuffer =dfdff; p [i] .FromMemory.BufferLength = 5; } HTTP_RESPONSE响应; ZeroMemory(& response,sizeof(HTTP_RESPONSE)); PCSTR Reason =OK; response.StatusCode = 200; response.pReason =原因; response.ReasonLength = strlen(Reason); ADD_KNOWN_HEADER(response,HttpHeaderContentType,text / html); ADD_KNOWN_HEADER(响应,HttpHeaderConnection,keep-alive); ADD_KNOWN_HEADER(响应,HttpHeaderTransferEncoding,chunked); ADD_KNOWN_HEADER(response,HttpHeaderContentLength,chLen); response.EntityChunkCoun = 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(响应,HttpHeaderTransferEncoding,chunked); 结果为0,我的回复发送给客户。 如何进行Chunked传输编码?解决方案 [更新回答客户端获取普通文本而不是分块数据的问题]: Chunked是一种传输方法。附加信息仅用于传输数据,不属于原始数据。当客户端调用某个API函数来获取接收到的HTTP数据时,此API函数将解码分块数据,添加具有解码数据大小的Content-Length头,并提供解码后的原始数据。 我改变了我的代码:这可以发送回复 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_CHUND chunk; chunk.DataChunkType = HttpDataChunkFromMemory; chunk.FromMemory.pBuffer = buffer [i]; // ---bufferLen\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_CHUND chunkEnd; chunkEnd.DataChunkType = HttpDataChunkFromMemory; chunkEnd.FromMemory.pBuffer =\\\\ nn0 \\\\ n; chunkEnd.FromMemory.BufferLength = 5; HttpSendResponseEntityBody(ReqQueueHandle,HttpRequest-> requestId, HTT_SEND_RESPONSE_FLAG_DISCONNECT,1,& chunkEnd,0 NULL,0,NULL,NULL); hii develop a Http server,my Summary of response code is this:PHTTP_DATA_CHUND p = new HTTP_DATA_CHUND[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.EntityChunkCoun = count;response.pEntityChunks=p;ULONG BytesSent;ULONG result = HttpSendHttpResponse(ReqQueueHandle, HttpRequest->RequestId, 0, &response, NULL,&BytesSent, NULL, 0,NULL,NULL, NULL);but the result is 87 !!!now if i remove this line of code:ADD_KNOWN_HEADER(response, HttpHeaderTransferEncoding, "chunked");the result is 0 and my response is sent to client.How i to make Chunked transfer encoding? 解决方案[UPDATE to answer the question why the client gets normal text rather the chunked data]:Chunked is a transfer method. The additional information is used only for transferring data and does not belong to the original data. When a client calls some API function to get received HTTP data, this API function will decode chunked data, add a Content-Length header with the size of the decoded data and provides the decoded original data. 这篇关于分块传输编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 23:17