本文整理汇总了C++中HttpResponse::getBody方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpResponse::getBody方法的具体用法?C++ HttpResponse::getBody怎么用?C++ HttpResponse::getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpResponse
的用法示例。
在下文中一共展示了HttpResponse::getBody方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: file
HttpJobRequest * HttpFileRequestHandler::handleHttpRequest(HttpRequest * request, const HttpServerTask *task)
{
FileUtils::paloifstream file(filename.c_str());
if (!file) {
HttpResponse * response = new HttpResponse(HttpResponse::NOT_FOUND);
response->getBody().appendText("file not found");
return new DirectHttpResponse(request->getRequestPath(), response);
}
HttpResponse * response = new HttpResponse(HttpResponse::OK);
response->setContentType(contentType);
int max = 1000;
char* c = new char[max];
while (!file.eof()) {
file.read(c, max);
response->getBody().appendText(c, (size_t)file.gcount());
}
delete[] c;
return new DirectHttpResponse(request->getRequestPath(), response);
}
示例2: submitBlock
bool RPCTestNode::submitBlock(const std::string& block) {
HttpRequest httpReq;
httpReq.setUrl("/json_rpc");
httpReq.addHeader("Host", "127.0.0.1:" + boost::lexical_cast<std::string>(m_rpcPort));
httpReq.addHeader("Content-Type", "application/json-rpc");
JsonValue request(cryptonote::JsonValue::OBJECT);
JsonValue jsonRpc;
jsonRpc = "2.0";
request.insert("jsonrpc", jsonRpc);
JsonValue methodString;
methodString = "submitblock";
request.insert("method", methodString);
JsonValue id;
id = "sync";
request.insert("id", id);
JsonValue params(JsonValue::ARRAY);
JsonValue blockstr;
blockstr = block.c_str();
params.pushBack(blockstr);
request.insert("params", params);
std::stringstream jsonOutputStream;
jsonOutputStream << request;
httpReq.setBody(jsonOutputStream.str());
TcpConnector connector(m_dispatcher, "127.0.0.1", m_rpcPort);
TcpConnection connection = connector.connect();
TcpStreambuf streambuf(connection);
std::iostream connectionStream(&streambuf);
LOG_DEBUG("invoke json-rpc: " + httpReq.getBody());
connectionStream << httpReq;
connectionStream.flush();
HttpResponse httpResp;
HttpParser parser;
parser.receiveResponse(connectionStream, httpResp);
connectionStream.flush();
if (httpResp.getStatus() != HttpResponse::STATUS_200) return false;
epee::serialization::portable_storage ps;
if (!ps.load_from_json(httpResp.getBody())) {
LOG_ERROR("cannot parse response from daemon: " + httpResp.getBody());
return false;
}
epee::json_rpc::response<COMMAND_RPC_SUBMITBLOCK::response, epee::json_rpc::error> jsonRpcResponse;
jsonRpcResponse.load(ps);
if (jsonRpcResponse.error.code || jsonRpcResponse.error.message.size()) {
LOG_ERROR("RPC call of submit_block returned error: " + TO_STRING(jsonRpcResponse.error.code) + ", message: " + jsonRpcResponse.error.message);
return false;
}
if (jsonRpcResponse.result.status != CORE_RPC_STATUS_OK) return false;
return true;
}
示例3: startMining
bool RPCTestNode::startMining(size_t threadsCount, const std::string& address) {
LOG_DEBUG("startMining()");
using namespace cryptonote;
COMMAND_RPC_START_MINING::request req;
COMMAND_RPC_START_MINING::response resp;
req.miner_address = address;
req.threads_count = threadsCount;
std::stringstream requestStream;
JsonOutputStreamSerializer enumerator;
enumerator(req, "");
requestStream << enumerator;
HttpRequest httpReq;
prepareRequest(httpReq, "/start_mining", requestStream.str());
HttpResponse httpResp;
sendRequest(httpReq, httpResp);
if (httpResp.getStatus() != HttpResponse::STATUS_200) return false;
std::stringstream responseStream(httpResp.getBody());
JsonInputStreamSerializer en(responseStream);
en(resp, "");
if (resp.status != CORE_RPC_STATUS_OK) {
std::cout << "startMining() RPC call fail: " << resp.status;
return false;
}
return true;
}
示例4: onReadComplete
void WebClientApp::onReadComplete()
{
mText.push_back( "Read complete" );
console() << "HTTP version: ";
switch ( mHttpResponse.getHttpVersion() ) {
case HttpVersion::HTTP_0_9:
console() << "0.9";
break;
case HttpVersion::HTTP_1_0:
console() << "1.0";
break;
case HttpVersion::HTTP_1_1:
console() << "1.1";
break;
case HttpVersion::HTTP_2_0:
console() << "2.0";
break;
}
console() << endl;
console() << "Status code: " << mHttpResponse.getStatusCode() << endl;
console() << "Reason: " << mHttpResponse.getReason() << endl;
console() << "Header fields: " << endl;
for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) {
console() << ">> " << kvp.first << ": " << kvp.second << endl;
}
console() << endl;
console() << "Body:" << endl;
console() << HttpResponse::bufferToString( mHttpResponse.getBody() ) << endl;
mSession->close();
}
示例5: handleDone
void HttpServerTask::handleDone()
{
// get response from job request
HttpResponse* response = httpJobRequest->getResponse();
if (response == 0) {
response = new HttpResponse(HttpResponse::NO_RESPONSE);
}
// trace
#ifdef ENABLE_TRACE_OPTION
{
string requestString = "";
if (httpRequest != 0) {
requestString = httpRequest->getHeaderString();
const string bs = httpRequest->getBodyString();
if (!bs.empty()) {
if (requestString.find("?") == string::npos) {
requestString += "?";
}
requestString += bs;
}
}
server->traceRequest(requestString + response->getBody().c_str());
}
#endif
// add response to output buffer
addResponse(response);
// we can now free the response
delete response;
// and the jobe request
delete httpJobRequest;
httpJobRequest = 0;
// and the http request
if (httpRequest != 0) {
httpRequest->releaseBuffers();
delete httpRequest;
httpRequest = 0;
}
httpRequestPending = false;
// remove body from read buffer and reset read position
readBuffer.erase_front(bodyPosition + bodyLength);
readPosition = 0;
bodyPosition = 0;
bodyLength = 0;
}
示例6: invokeJsonRpcCommand
void invokeJsonRpcCommand(HttpClient& httpClient, JsonRpcRequest& jsReq, JsonRpcResponse& jsRes) {
HttpRequest httpReq;
HttpResponse httpRes;
httpReq.setUrl("/json_rpc");
httpReq.setBody(jsReq.getBody());
httpClient.request(httpReq, httpRes);
if (httpRes.getStatus() != HttpResponse::STATUS_200) {
throw std::runtime_error("JSON-RPC call failed, HTTP status = " + std::to_string(httpRes.getStatus()));
}
jsRes.parse(httpRes.getBody());
JsonRpcError err;
if (jsRes.getError(err)) {
throw err;
}
}
示例7: retrieveRemoteVersion
int VersionChecker::retrieveRemoteVersion(const std::string& urlToVersion) {
Http http(urlToVersion, 0.5);
HttpResponse response;
if (this->quiet) {
http.enableQuiet();
} else {
http.disableQuiet();
}
if (!this->quiet) {
fprintf(stderr, "retrieve remote version from [ %s ]\n",
urlToVersion.c_str());
}
// 2: the remote version file has two lines
int ret = http.read(&response, 2);
if (ret < 0) {
if (!this->quiet) {
fprintf(
stderr,
"Failed to retrieve remote version, probably due to wrong http\n");
}
return -1;
}
this->remoteInformation = response.getBody();
if (this->remoteInformation.size() < 1) {
if (!this->quiet) {
fprintf(stderr,
"Failed to retrieve remote version due to empty version "
"information\n");
}
return -1;
}
this->remoteVersion = this->remoteInformation[0];
if (!this->quiet) {
fprintf(stderr, "Retrieved remote version [ %s ]\n",
this->remoteVersion.c_str());
}
return 0;
}
示例8: stopDaemon
bool RPCTestNode::stopDaemon() {
LOG_DEBUG("stopDaemon()");
using namespace cryptonote;
COMMAND_RPC_STOP_DAEMON::request req;
COMMAND_RPC_STOP_DAEMON::response resp;
std::stringstream requestStream;
JsonOutputStreamSerializer enumerator;
enumerator(req, "");
requestStream << enumerator;
HttpRequest httpReq;
prepareRequest(httpReq, "/stop_daemon", requestStream.str());
HttpResponse httpResp;
sendRequest(httpReq, httpResp);
if (httpResp.getStatus() != HttpResponse::STATUS_200) return false;
std::stringstream responseStream(httpResp.getBody());
JsonInputStreamSerializer en(responseStream);
en(resp, "");
if (resp.status != CORE_RPC_STATUS_OK) {
std::cout << "stopDaemon() RPC call fail: " << resp.status;
return false;
}
return true;
}
示例9: onRead
void HttpClientApp::onRead( ci::BufferRef buffer )
{
size_t sz = buffer->getSize();
mBytesRead += sz;
mText.push_back( toString( sz ) + " bytes read" );
if ( !mHttpResponse.hasHeader() ) {
// Parse header
mHttpResponse.parseHeader( HttpResponse::bufferToString( buffer ) );
buffer = HttpResponse::removeHeader( buffer );
// Get content-length
for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) {
if ( kvp.first == "Content-Length" ) {
mContentLength = fromString<size_t>( kvp.second );
break;
}
}
}
// Append buffer to body
mHttpResponse.append( buffer );
if ( mBytesRead < mContentLength ) {
// Keep reading until we hit the content length
mSession->read();
} else {
mText.push_back( "Read complete" );
mText.push_back( toString( mHttpResponse.getStatusCode() ) + " " + mHttpResponse.getReason() );
if ( mHttpResponse.getStatusCode() == 200 ) {
for ( const KeyValuePair& kvp : mHttpResponse.getHeaders() ) {
// Choose file extension based on MIME type
if ( kvp.first == "Content-Type" ) {
string mime = kvp.second;
if ( mime == "audio/mp3" ) {
mFilename += ".mp3";
} else if ( mime == "image/jpeg" ) {
mFilename += ".jpg";
} else if ( mime == "image/png" ) {
mFilename += ".png";
}
} else if ( kvp.first == "Connection" ) {
// Close connection if requested by server
if ( kvp.second == "close" ) {
mSession->close();
}
}
}
// Save the file
fs::path path = getAppPath();
#if !defined ( CINDER_MSW )
path = path.parent_path();
#endif
path = path / mFilename;
OStreamFileRef file = writeFileStream( path );
file->write( mHttpResponse.getBody() );
mText.push_back( mFilename + " downloaded" );
} else {
// Write error
mText.push_back( "Response: " + HttpResponse::bufferToString( mHttpResponse.getBody() ) );
mSession->close();
}
}
}