0
私はboost :: asioの基本例を使ってenpointに接続し、json応答を取得します。文字列が戻ったときにここでコードboost :: asio文字列としてstreambufレスポンスを取得しようとすると問題が発生する
boost::asio::streambuf response;
boost::asio::read_until(socket_, response, "\r\n");
// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
LOG_ERROR << "invalid response";
return -1;
}
resp_status_code_ = status_code;
resp_status_code_ = status_code;
if (status_code != 200) {
LOG_ERROR << "response did not returned 200 but " << status_code;
return -1;
}
//read until the response headers termination
boost::asio::read_until(socket_, response, "\r\n\r\n");
//read the headers.
std::string header;
while (std::getline(response_stream, header) && header != "\r") {
;
}
std::ostringstream ostringstream_content;
if (response.size() > 0)
ostringstream_content << &response;
boost::system::error_code error;
boost::system::error_code error;
while (true) {
size_t n = boost::asio::read(socket_, response, boost::asio::transfer_at_least(1), error);
if (!error)
{
if (n)
ostringstream_content << &response;
}
if (error == boost::asio::error::eof)
break;
if (error)
return -1; //throw boost::system::system_error(error);
}
str_response_ = ostringstream_content.str();
は、しかし、私はこの
1 [5a0
2 {"a_token":"eyJhbGciOiJSUzI1NiJ9.eyJleHAiOjE0Nzk5Mjk2NjcsIn...030"}
3 0
4
5 ]
[と]を読んでいますされ、対応を開始し、データの終わりをデバッグするために私のコードからマークされています。私は1行目に問題のある文字が3つ以上あります。つまり、正しい2行目のjson文字列である5a0、そして はゼロと空白行です。どのようにしてjson文字列だけを正確に取得できますか?
変更しましたか?私は多かれ少なかれ同じコードを使用していると思います。 – cateof
ヘッダ解析以外は何もありません。また、私はasioのスタンドアロンバージョンを使用しています。うまくいけば、何の違いもありません。 – Arunmu
@cateof上記のような完全な例を与えることができれば、おそらく分かりやすいでしょう。 – Arunmu