restsdk安装及使用

1、安装相关的库,如boost、openssl
  • 可以直接用windows中vcpkg工具安装但经常网络会出错,推荐使用离线包直接安装,即使用管理员ps页面,切换到vcpkg离线包目录执行bootstrap-vcpkg.bat文件、等待环境安装完成
    在这里插入图片描述
vcpkg install --triplet x64-windows zlib openssl boost-system boost-date-time boost-regex boost-interprocess websocketpp brotli
2、编译cpprestdsk源码(这步可以省略)
  • 先安装cmake,cmake安装过程百度一搜一大堆,这里就不详细阐述。进入powershell中切换到cpprestsdk-master目录中执行以下命令,其中C:/Users/YSQ/Desktop/vcpkg/scripts/buildsystems/vcpkg.cmake是刚才安装vcpkg文件中vcpkg.cmake的路径
64位安装命令:cmake Release -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/Users/YSQ/Desktop/vcpkg/scripts/buildsystems/vcpkg.cmake
32位安装命令:cmake Release -A Win32 -DCMAKE_TOOLCHAIN_FILE=C:/Users/YSQ/Desktop/vcpkg/scripts/buildsystems/vcpkg.cmake
  • 安装完成之后cpprestsdk-master中会生成解决方案,使用vs编译即可。
3、直接进入主题,如何在vs中使用restsdk
3.1、VS2017
  • 在线添加包

    vs2017直接在NuGet包管理器中添加cpprestsdk,我使用的版本是cpprestsdk.v.141.2.10.7,因为这个版本能够在vs2015中离线添加使用,
    在这里插入图片描述
    在这里插入图片描述

    • 运行一个官方demo,引入头文件无误,运行成功
      在这里插入图片描述
3.2、添加离线包
  • 离线包在NuGet包管理器设置中添加一个本地源,随后选择填入你放包的路径,我把包放在这个位置C:\Users\YSQ\AppData\Local\NuGet。这部完成之后在程序包源选择你刚添加的本地包源,安装即可,系统会自动下载添加依赖的包。
    在这里插入图片描述
    在这里插入图片描述

  • VS2015中安装在线包会出错读取不到头文件,2015还是按照离线包的过程添加

  • 官方demo

/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * searchfile.cpp - Simple cmd line application that uses a variety of streams features to search a file,
 *      store the results, and write results back to a new file.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "cpprest/containerstream.h"
#include "cpprest/filestream.h"
#include "cpprest/producerconsumerstream.h"

using namespace utility;
using namespace concurrency::streams;

/// <summary>
/// A convenient helper function to loop asychronously until a condition is met.
/// </summary>
pplx::task<bool> _do_while_iteration(std::function<pplx::task<bool>(void)> func)
{
    pplx::task_completion_event<bool> ev;
    func().then([=](bool guard) { ev.set(guard); });
    return pplx::create_task(ev);
}
pplx::task<bool> _do_while_impl(std::function<pplx::task<bool>(void)> func)
{
    return _do_while_iteration(func).then([=](bool guard) -> pplx::task<bool> {
        if (guard)
        {
            return ::_do_while_impl(func);
        }
        else
        {
            return pplx::task_from_result(false);
        }
    });
}
pplx::task<void> do_while(std::function<pplx::task<bool>(void)> func)
{
    return _do_while_impl(func).then([](bool) {});
}

/// <summary>
/// Structure used to store individual line results.
/// </summary>
typedef std::vector<std::string> matched_lines;
namespace Concurrency
{
namespace streams
{
/// <summary>
/// Parser implementation for 'matched_lines' type.
/// </summary>
template<typename CharType>
class type_parser<CharType, matched_lines>
{
public:
    static pplx::task<matched_lines> parse(streambuf<CharType> buffer)
    {
        basic_istream<CharType> in(buffer);
        auto lines = std::make_shared<matched_lines>();
        return do_while([=]() {
                   container_buffer<std::string> line;
                   return in.read_line(line).then([=](const size_t bytesRead) {
                       if (bytesRead == 0 && in.is_eof())
                       {
                           return false;
                       }
                       else
                       {
                           lines->push_back(std::move(line.collection()));
                           return true;
                       }
                   });
               })
            .then([=]() { return matched_lines(std::move(*lines)); });
    }
};
} // namespace streams
} // namespace Concurrency
/// <summary>
/// Function to create in data from a file and search for a given string writing all lines containing the string to
/// memory_buffer.
/// </summary>
static pplx::task<void> find_matches_in_file(const string_t& fileName,
                                             const std::string& searchString,
                                             basic_ostream<char> results)
{
    return file_stream<char>::open_istream(fileName).then([=](basic_istream<char> inFile) {
        auto lineNumber = std::make_shared<int>(1);
        return ::do_while([=]() {
                   container_buffer<std::string> inLine;
                   return inFile.read_line(inLine).then([=](size_t bytesRead) {
                       if (bytesRead == 0 && inFile.is_eof())
                       {
                           return pplx::task_from_result(false);
                       }

                       else if (inLine.collection().find(searchString) != std::string::npos)
                       {
                           results.print("line ");
                           results.print((*lineNumber)++);
                           return results.print(":")
                               .then([=](size_t) {
                                   container_buffer<std::string> outLine(std::move(inLine.collection()));
                                   return results.write(outLine, outLine.collection().size());
                               })
                               .then([=](size_t) { return results.print("\r\n"); })
                               .then([=](size_t) { return true; });
                       }

                       else
                       {
                           ++(*lineNumber);
                           return pplx::task_from_result(true);
                       }
                   });
               })
            .then([=]() {
                // Close the file and results stream.
                return inFile.close() && results.close();
            });
    });
}

/// <summary>
/// Function to write out results from matched_lines type to file
/// </summary>
static pplx::task<void> write_matches_to_file(const string_t& fileName, matched_lines results)
{
    // Create a shared pointer to the matched_lines structure to copying repeatedly.
    auto sharedResults = std::make_shared<matched_lines>(std::move(results));

    return file_stream<char>::open_ostream(fileName, std::ios::trunc).then([=](basic_ostream<char> outFile) {
        auto currentIndex = std::make_shared<size_t>(0);
        return ::do_while([=]() {
                   if (*currentIndex >= sharedResults->size())
                   {
                       return pplx::task_from_result(false);
                   }

                   container_buffer<std::string> lineData((*sharedResults)[(*currentIndex)++]);
                   outFile.write(lineData, lineData.collection().size());
                   return outFile.print("\r\n").then([](size_t) { return true; });
               })
            .then([=]() { return outFile.close(); });
    });
}

#ifdef _WIN32
int wmain(int argc, wchar_t* args[])
#else
int main(int argc, char* args[])
#endif
{
    if (argc != 4)
    {
        printf("Usage: SearchFile.exe input_file search_string output_file\n");
        return -1;
    }
    const string_t inFileName = args[1];
    const std::string searchString = utility::conversions::to_utf8string(args[2]);
    const string_t outFileName = args[3];
    producer_consumer_buffer<char> lineResultsBuffer;

    // Find all matches in file.
    basic_ostream<char> outLineResults(lineResultsBuffer);
    find_matches_in_file(inFileName, searchString, outLineResults)

        // Write matches into custom data structure.
        .then([&]() {
            basic_istream<char> inLineResults(lineResultsBuffer);
            return inLineResults.extract<matched_lines>();
        })

        // Write out stored match data to a new file.
        .then([&](matched_lines lines) { return write_matches_to_file(outFileName, std::move(lines)); })

        // Wait for everything to complete.
        .wait();

    return 0;
}

补充如果包添加完之后还是显示头文件不正确,手动添加库文件,步骤如下

在vc++目录中的包含目录和库目录分别输入刚安装好的包里面include和lib路径,然后再链接器中添加库文件即可在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于restsdk服务器测试,我们通常会采取以下步骤: 1. 安装和配置restsdk服务器:首先,我们需要在服务器上安装并配置restsdk,确保其能够正常运行。这包括下载并安装所需的软件和依赖项,并设置服务器的环境变量等。 2. 编写测试用例:根据需求和服务器的接口规范,我们需要编写一系列的测试用例来验证服务器的功能和性能。这些测试用例应该涵盖各种情况,包括正常输入、异常输入、并发访问等。 3. 执行测试用例:通过使用自动化测试工具,如JUnit或Postman等,我们可以执行编写的测试用例。这些工具可以模拟客户端发送请求到服务器,并检查响应是否符合预期。我们可以使用不同的测试数据和参数来覆盖服务器的各种情况。 4. 分析测试结果:一旦测试用例执行完成,我们需要对测试结果进行分析。我们应该检查每个测试用例的通过与否,并记录失败的用例。根据失败的原因,我们可以定位和修复服务器中的问题。 5. 优化和重复测试:根据测试结果和反馈,我们可以优化服务器的性能和功能。优化可能包括调整服务器的配置、优化网络传输等。然后,我们可以重复执行测试用例,确保服务器在改进后是否满足了预期的性能和功能要求。 通过以上步骤,我们可以全面地测试和评估restsdk服务器的功能和性能。这有助于发现和解决潜在的问题,并确保服务器能够正常运行和满足用户的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值