代码之家  ›  专栏  ›  技术社区  ›  Hafiz H

Boost中的空体::ASIO HTTP POST

  •  0
  • Hafiz H  · 技术社区  · 8 年前

    我试着用下面的代码使用Post方法。在服务器端,主体总是空的。

    tcp::endpoint ip_port(address::from_string(host), port);
    socket.connect(ip_port);
    
    boost::asio::streambuf request;
    std::ostream request_stream(&request);
    
    request_stream << "POST /myservice HTTP/1.1\n\n";
    request_stream << "Host:" << "host:port" << "\r\n";
    request_stream << "User-Agent: C/1.0" << "\r\n";
    request_stream << "Content-Type: application/json; charset=utf-8\r\n";
    request_stream << "Accept: */*\r\n";
    request_stream << "Content-Length: ";
    request_stream << json.length() + "\r\n";
    request_stream << "Connection: close\r\n\r\n"; 
    request_stream << json;
    
    boost::asio::write(socket, request);
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   Hafiz H    8 年前

    最后,我找到了。第一行中的问题。如果有两个,则请求在此结束。我尝试了以下代码,并且能够在服务器端获得json主体。

    requestStream << "POST " << "/myservice" << " HTTP/1.1\r\n";
    requestStream << "Host: " << "myhost" << "\r\n";
    requestStream << "Accept: application/json\r\n";
    requestStream << "Content-Type: application/json; charset=UTF-8\r\n";
    requestStream << "Content-Length: " << json.length() << "\r\n";
    requestStream << "\r\n";
    requestStream << json << "\n\n";