問題描述
我只是想獲取帶有標(biāo)題的頁面的內(nèi)容...但是對于通過的最后一個信息包來說,我的大小為 1024 的緩沖區(qū)似乎太大或太小...我不知道不想得到太多或太少,如果這是有道理的.這是我的代碼.它可以很好地打印出包含所有信息的頁面,但我想確保它是正確的.
I'm just trying to get the contents of a page with their headers...but it seems that my buffer of size 1024 is either too large or too small for the last packet of information coming through...I don't want to get too much or too little, if that makes sense. Here's my code. It's printing out the page just fine with all the information, but I want to ensure that it's correct.
//Build HTTP Get Request
std::stringstream ss;
ss << "GET " << url << " HTTP/1.0
Host: " << strHostName << "
";
std::string req = ss.str();
// Send Request
send(hSocket, req.c_str(), strlen(req.c_str()), 0);
// Read from socket into buffer.
do
{
nReadAmount = read(hSocket, pBuffer, sizeof pBuffer);
printf("%s", pBuffer);
}
while(nReadAmount != 0);
推薦答案
讀取 HTTP 回復(fù)的正確方法是讀取直到收到完整的 LF
分隔行(某些服務(wù)器使用 bare LF
即使官方規(guī)范說使用 CRLF
),其中包含響應(yīng)代碼和版本,然后繼續(xù)閱讀 LF 分隔的行,即標(biāo)題,直到遇到一個長度為 0 的行,指示標(biāo)頭的結(jié)尾,然后您必須分析標(biāo)頭以找出剩余數(shù)據(jù)的編碼方式,以便您知道讀取它的正確方法并知道它是如何終止的.有幾種不同的可能性,請參閱 RFC 2616 第 4.4 節(jié)實(shí)際規(guī)則.
The correct way to read an HTTP reply is to read until you have received a full LF
-delimited line (some servers use bare LF
even though the official spec says to use CRLF
), which contains the response code and version, then keep reading LF-delimited lines, which are the headers, until you encounter a 0-length line, indicating the end of the headers, then you have to analyze the headers to figure out how the remaining data is encoded so you know the proper way to read it and know how it is terminated. There are several different possibilities, refer to RFC 2616 Section 4.4 for the actual rules.
換句話說,你的代碼需要改用這種結(jié)構(gòu)(偽代碼):
In other words, your code needs to use this kind of structure instead (pseudo code):
// Send Request
send(hSocket, req.c_str(), req.length(), 0);
// Read Response
std::string line = ReadALineFromSocket(hSocket);
int rescode = ExtractResponseCode(line);
std::vector<std::string> headers;
do
{
line = ReadALineFromSocket(hSocket);
if (line.length() == 0) break;
headers.push_back(line);
}
while (true);
if (
((rescode / 100) != 1) &&
(rescode != 204) &&
(rescode != 304) &&
(request is not "HEAD")
)
{
if ((headers has "Transfer-Encoding") && (Transfer-Encoding != "identity"))
{
// read chunks until a 0-length chunk is encountered.
// refer to RFC 2616 Section 3.6 for the format of the chunks...
}
else if (headers has "Content-Length")
{
// read how many bytes the Content-Length header says...
}
else if ((headers has "Content-Type") && (Content-Type == "multipart/byteranges"))
{
// read until the terminating MIME boundary specified by Content-Type is encountered...
}
else
{
// read until the socket is disconnected...
}
}
這篇關(guān)于使用 C++ Socket 只接收必要的數(shù)據(jù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!