【问题标题】:http_client of cpprestsdk/casablancacpprestsdk/casablanca 的 http_client
【发布时间】:2018-04-17 01:47:49
【问题描述】:

我有 api https://api.gm-system.net/api/authenticate/searchStaffs/searchText,它返回一个人员列表。

这是我使用 cpprestsdk 和 c++ 访问此 api 的代码。

auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/michael"));


        return client.request(methods::GET);
    })

        // Handle response headers arriving.
        .then([=](http_response response)
    {
       ......
    }

如果没问题的话。但是我只是手动输入"michael" searchText

我怎样才能让它接受任何类似这样的 searchText。

void MyTest(std::string searchText)
{
..... code here

// Create http_client to send the request.
http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/" + searchText));

return client.request(methods::GET);

..... code here
}

我已经试过了,它不起作用。 “U”宏有一些问题。 来自https://github.com/Microsoft/cpprestsdk/wiki/FAQU macro的描述是:

The 'U' macro can be used to create a string literal of the platform type. If you are using a library causing conflicts with the 'U' macro, for example Boost.Iostreams it can be turned off by defining the macro '_TURN_OFF_PLATFORM_STRING' before including the C++ REST SDK header files.

如果我将光标指向U,则错误提示:

no operator "+" matches these operands operand types are; const wchar_t[57] + const std::string

我希望有人可以帮助我。谢谢。

【问题讨论】:

标签: c++ casablanca cpprest-sdk


【解决方案1】:

自从

C++ REST SDK 使用不同的字符串类型,具体取决于 目标平台。例如对于 Windows 平台 在 Linux std::string 上,utility::string_t 是使用 UTF-16 的 std::wstring 使用 UTF-8。

您应该在需要时使用utility::string_t 类,并且不要将它与std::stringconst char * 混合使用(并在需要文字时使用U 宏)。

换句话说,您的函数应该接受utility::string_t 作为其searchText 参数(而不是std::string):

void MyTest(utility::string_t searchText)
{
    http_client client(U("https://api.gm-system.net/api/authenticate/searchStaffs/") + searchText);

    // etc ...

}

像这样使用它:

int main()
{

    utility::string_t searchText = U("Michael");
    MyTest(searchText);

    return 0;
}

如果必须从特定于平台的上下文中调用函数,则可以使用相应的 std 类型作为传入的参数类型(即在 Windows 上使用 std::wstring):

std::wstring searchText = L"Michael";
MyTest(searchText);

【讨论】:

  • 谢谢,但两种解决方案都不起作用。在第二个解决方案中,错误显示:error C2664: 'web::http::client::http_client::http_client(const web::http::client::http_client &)': cannot convert argument 1 from 'std:: basic_string,std::allocator>' 到 'const web::uri &'。
  • 好的,我想我解决了这个谜团(显然我在 linux 上)。
  • 谢谢先生。它解决了这个问题。但是我从我的 CLR 项目中调用了“MyTest()”函数,但我不能在那里使用“实用程序”。如果我愿意,我会回到关于“互斥”的错误。是否有类似使用 .NET 的 CLR 项目的实用程序?
  • @noyruto88 请检查我的编辑,我希望它能正常工作。
  • 还是一样的先生。无论如何,我正在从 String ^ searchText = searchBox->Text; 获取数据。你知道我需要什么转换才能将数据传递给 MyTest(searchText)?
猜你喜欢
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多