【问题标题】:can I reply html file as response with cpprest?我可以用 cpprest 回复 html 文件作为响应吗?
【发布时间】:2017-10-22 20:39:49
【问题描述】:

我将 cpprest 用于小型 http web 服务

当来自客户端的请求到来时,我想将服务器回复响应作为 html 文件。

喜欢:

//Webserver.cpp

...

void HandleGet(http_request request)
{
    uri relativeUri = request.relative_uri();
    utility::string_t path = relativeUri.path();

    ... some work ...

    //set response instance
    http_response response(status_codes::OK);
    response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
    response.headers().set_content_type(L"text/html");

    response.set_body( ***** );    // maybe html file contents be here

    request.reply(response);
}

还有一个..

如果这个想法有效,那么 html 文件将存在于哪里?

cpp文件是否存在同一目录?

请帮帮我..

【问题讨论】:

  • html会被发送到客户端,你没有保存在本地。

标签: html c++ http cpprest-sdk


【解决方案1】:

html 将被发送到客户端,您没有将其保存在本地。

【讨论】:

  • 好吧..当我在网络浏览器中输入127.0.0.1:8080/index.html时,我认为服务器应用程序应该回复index.html的内容。所以服务器没有index.html的文件吗?
  • 不,内容是服务器生成的。无需创建实际的 index.html 文件。只需转到 127.0.0.1:8080
  • 如果我想在index.html中添加自定义内容怎么办?(如链接到其他页面,一些图像,..等等)服务器如何生成它?
  • 你自己回答了... response.set_body( ***** ); // 也许html文件内容在这里
  • then.. 你能告诉我如何获取 html 文件的内容吗?我正在考虑使用ifstream 读取文件,但我认为这不是一个优雅的想法..
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多