Postupy: Práce s asynchronními datovými proudy (C++ REST SDK)
REST SDK C++ (kódový název "Casablanca") poskytuje funkce datového proudu, která umožňuje snadněji pracovat s protokolem TCP sockets, souborů na disku a paměti.Datové proudy C++ REST SDK podobat protokolům poskytovaným standardní knihovny C++ s tím rozdílem, že C++ REST SDK verze proveďte využívání asynchronního přístupu.Vrátí knihovně pplx::taska nikoli hodnotu přímo, pro vstupně-výstupní operace, které může potenciálně blokovat.Tato stránka zobrazuje dva příklady.První ukazuje, jak pro zápis do a čtení z datového proudu s použitím STL kontejnery a nezpracovaná paměti.Druhý příklad vytvoří požadavek HTTP GET a vytiskne část jeho datového proudu odpovědi ke konzole.
![]() |
---|
Toto téma obsahuje informace o rozhraní C++ REST SDK 1.0 (kódový název "Casablanca").Pokud používáte novější verze z Codeplex Casablanca webové stránky, pak použijte místní dokumentaci na adrese http://casablanca.codeplex.com/documentation. |
Více Úplný příklad, který se zobrazuje #include a using příkazy dodržovat tyto příklady.
Chcete-li pracovat s datovými proudy spolu s kontejnery STL a nezpracovaná paměti
Tento příklad ukazuje, jak ke čtení a zápisu z datového proudu s použitím STL kontejnery a nezpracovaná paměti.
// Shows how to read from and write to a stream with an STL container or raw pointer.
void ReadWriteStream(istream inStream, ostream outStream)
{
// Write a string to the stream.
std::string strData("test string to write\n");
container_buffer<std::string> outStringBuffer(std::move(strData));
outStream.write(outStringBuffer, outStringBuffer.collection().size()).then([](size_t bytesWritten)
{
// Perform actions here once the string has been written...
});
// Read a line from the stream into a string.
container_buffer<std::string> inStringBuffer;
inStream.read_line(inStringBuffer).then([inStringBuffer](size_t bytesRead)
{
const std::string &line = inStringBuffer.collection();
// Perform actions here after reading line into a string...
});
// Write data from a raw chunk of contiguous memory to the stream.
// The raw data must stay alive until write operation has finished.
// In this case we will place on the heap to avoid any issues.
const size_t rawDataSize = 8;
unsigned char* rawData = new unsigned char[rawDataSize];
memcpy(&rawData[0], "raw data", rawDataSize);
rawptr_buffer<unsigned char> rawOutBuffer(rawData, rawDataSize, std::ios::in);
outStream.write(rawOutBuffer, rawDataSize).then([rawData](size_t bytesWritten)
{
delete []rawData;
// Perform actions here once the string as been written...
});
}
Pro přístup k datovému proudu odpovědi HTTP
Zde je použití web::http::http_response::body metoda pro načtení concurrency::streams::istream objektu načtena data ze.V tomto příkladu pro jednoduchost, vytiskne pouze několika prvních znaků odpovědi ke konzole.Více základní verze, který načte odpověď serveru, ale nefunguje s datového proudu odpovědi, naleznete v části postup: připojení k serverům HTTP.
// Creates an HTTP request and prints part of its response stream.
pplx::task<void> HTTPStreamingAsync()
{
http_client client(L"https://www.fourthcoffee.com");
return client.request(methods::GET).then([](http_response response)
{
if(response.status_code() != status_codes::OK)
{
// Handle error cases...
return pplx::task_from_result();
}
// Perform actions here reading from the response stream...
// In this example, we print the first 15 characters of the response to the console.
istream bodyStream = response.body();
container_buffer<std::string> inStringBuffer;
return bodyStream.read(inStringBuffer, 15).then([inStringBuffer](size_t bytesRead)
{
const std::string &text = inStringBuffer.collection();
// For demonstration, convert the response text to a wide-character string.
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv;
std::wostringstream ss;
ss << utf16conv.from_bytes(text.c_str()) << std::endl;
std::wcout << ss.str();
});
});
/* Output:
<!DOCTYPE html>
*/
}
Úplný příklad
Zde je úplný příklad.
#include <codecvt>
#include <containerstream.h>
#include <http_client.h>
#include <iostream>
#include <producerconsumerstream.h>
#include <rawptrstream.h>
using namespace concurrency;
using namespace concurrency::streams;
using namespace web::http;
using namespace web::http::client;
// Shows how to read from and write to a stream with an STL container or raw pointer.
void ReadWriteStream(istream inStream, ostream outStream)
{
// Write a string to the stream.
std::string strData("test string to write\n");
container_buffer<std::string> outStringBuffer(std::move(strData));
outStream.write(outStringBuffer, outStringBuffer.collection().size()).then([](size_t bytesWritten)
{
// Perform actions here once the string has been written...
});
// Read a line from the stream into a string.
container_buffer<std::string> inStringBuffer;
inStream.read_line(inStringBuffer).then([inStringBuffer](size_t bytesRead)
{
const std::string &line = inStringBuffer.collection();
// Perform actions here after reading line into a string...
});
// Write data from a raw chunk of contiguous memory to the stream.
// The raw data must stay alive until write operation has finished.
// In this case we will place on the heap to avoid any issues.
const size_t rawDataSize = 8;
unsigned char* rawData = new unsigned char[rawDataSize];
memcpy(&rawData[0], "raw data", rawDataSize);
rawptr_buffer<unsigned char> rawOutBuffer(rawData, rawDataSize, std::ios::in);
outStream.write(rawOutBuffer, rawDataSize).then([rawData](size_t bytesWritten)
{
delete []rawData;
// Perform actions here once the string as been written...
});
}
// Creates an HTTP request and prints part of its response stream.
pplx::task<void> HTTPStreamingAsync()
{
http_client client(L"https://www.fourthcoffee.com");
return client.request(methods::GET).then([](http_response response)
{
if(response.status_code() != status_codes::OK)
{
// Handle error cases...
return pplx::task_from_result();
}
// Perform actions here reading from the response stream...
// In this example, we print the first 15 characters of the response to the console.
istream bodyStream = response.body();
container_buffer<std::string> inStringBuffer;
return bodyStream.read(inStringBuffer, 15).then([inStringBuffer](size_t bytesRead)
{
const std::string &text = inStringBuffer.collection();
// For demonstration, convert the response text to a wide-character string.
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf16conv;
std::wostringstream ss;
ss << utf16conv.from_bytes(text.c_str()) << std::endl;
std::wcout << ss.str();
});
});
/* Output:
<!DOCTYPE html>
*/
}
int wmain()
{
// This example uses the task::wait method to ensure that async operations complete before the app exits.
// In most apps, you typically don�t wait for async operations to complete.
streams::producer_consumer_buffer<uint8_t> buffer;
//ReadWriteStream(buffer.create_istream(), buffer.create_ostream());
HTTPStreamingAsync().wait();
}