簡體   English   中英

Boost Asio收到未知號碼?

[英]Boost Asio receive unknown Number?

我寫了一個Http / Rest客戶端。

主要問題是我在請求的數據中收到了一些未知數字。 我真的不知道它們來自哪里。

e0b
<html>
<head>
[...]
</body>
</html>
0 

您會看到e0b和結尾處的0 例如在大的xml文件中,我得到的是這樣的:

<sometag id="somei
2000
d"><child>
...
</child></some
2000
tag>

這是我無法復制的。

我的代碼:

  // read the response status code
  boost::asio::streambuf httpStreamBufferResponse;
  boost::asio::read_until(httpSocket, httpStreamBufferResponse, "\r\n");

  // check status code and validate
  istream httpResponseIStream(&httpStreamBufferResponse);

  // temp var for version
  string sHttpVersion;
  httpResponseIStream >> sHttpVersion;

  // temp var for status code
  unsigned int uiStatusCode;
  httpResponseIStream >> uiStatusCode;

  // fetch status message and switch it
  string sStatusMessage;
  getline(httpResponseIStream, sStatusMessage);
  if(!httpResponseIStream || sHttpVersion.substr(0, 5) != "HTTP/"){
    new Note(eNotesType(ERROR), "Request Interrupt", "Invalid Request Response");
    Log::write("ERROR: Request Interrupt: Invalid Request Response");
  }
  // != 200 even means that something is not OK
  if(uiStatusCode != 200){
    this -> sHttpStatusCode = uiStatusCode;
    new Note(eNotesType(WARNING), "Request Response " 
      + boost::lexical_cast<string>(uiStatusCode), httpErrorToString.at(uiStatusCode));
    Log::write("WARNING: Request Response " 
      + boost::lexical_cast<string>(uiStatusCode) + ": " + httpErrorToString.at(uiStatusCode));
  }

  // Read the response headers, which are terminated by a blank line.
  boost::asio::read_until(httpSocket, httpStreamBufferResponse, "\r\n\r\n");

  // Process the response header
  stringstream responseSStream;
  string responseSHeader;
  while (getline( httpResponseIStream, responseSHeader ) && responseSHeader != "\r" ) {
    responseSStream << responseSHeader;
  }
  // store header in member variable
  this -> sHttpResponseHeader = sHttpVersion + " " + boost::lexical_cast<string>(uiStatusCode) + " " 
    + httpErrorToString.at(uiStatusCode) + "\n" + responseSStream.str();

  // read until EOF and writing data to output as we go.
  ostringstream responseOSStream;
  while(boost::asio::read(httpSocket, httpStreamBufferResponse, boost::asio::transfer_at_least(1), error)){
    responseOSStream << &httpStreamBufferResponse;
  }

  // store content in member variable
  this -> sHttpResponseContent = responseOSStream.str();

  // if there is no EOF
  if(error != boost::asio::error::eof){ 
    new Note(eNotesType(ERROR), "Request Interrupt", "Invalid Response End");
    Log::write("ERROR: Request Interrupt: Invalid  Response End");    
  }

// catch not known exceptions properly
} catch (exception& e){
  string exceptionMessage = e.what();
  new Note(eNotesType(ERROR), "Exception", exceptionMessage);
  Log::write("ERROR: Exception: " + exceptionMessage);    
}

// log http standby
Log::write("http status: standby");

這將是一個非常高興,如果有人有任何想法,這來自..?!

我緊張不安。

您的代碼聲稱符合HTTP / 1.1,實際上不符合HTTP / 1.1的要求。 不要聲稱符合HTTP / 1.1,或者不要確保您的代碼可以執行標准要求客戶端執行的所有操作。

所有HTTP / 1.1應用程序必須能夠接收和解碼“分塊”傳輸編碼,並且必須忽略它們不理解的塊擴展名。 -HTTP / 1.1規范,第3.6.1節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM