【问题标题】:C++ Http POST 400 bad requestC++ Http POST 400 错误请求
【发布时间】:2018-02-20 09:29:31
【问题描述】:

直到最近,以下代码才能够在我的(未更改的)服务器上执行发布请求。几周以来,我收到了 400 个错误的请求响应。可能是什么问题呢?使用命令行 curl 的 POST 发送工作正常。

std::ostringstream oss;
oss << "mydata";

int size = oss.tellp();
std::string test = oss.str();

boost::asio::ip::tcp::iostream stream;
stream.connect("myserver.nl", "http");
stream << "POST /dir/newdata.php  HTTP/1.1\r\n";
stream << "Host: myserver.nl\r\n";
stream << "Accept: */*\r\n";
stream << "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
stream << "Content-Length: " << size << "\r\n";
stream << "Connection: close\r\n\r\n";

stream << oss.str();
stream.flush();
std::cout  << stream.rdbuf();

现在会导致以下错误:

HTTP/1.1 400 Bad Request
Date: Tue, 20 Feb 2018 09:22:15 GMT
Server: Apache/2.4.29 (Unix)
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br/>
</p>

【问题讨论】:

    标签: c++ http boost boost-asio


    【解决方案1】:

    查询和 HTTP 版本之间有一个额外的空格:

    stream << "POST /dir/newdata.php HTTP/1.1\r\n";
    

    【讨论】:

    • 啊,是的,这似乎是问题所在!这么多小时浪费在一个空间上。奇怪的是,这几个月都运行良好。如果解决了问题,我会尽快验证并接受答案。
    • 不合规/不严格的服务器可能接受了它。更有可能是你改变了空间:)
    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2015-09-27
    相关资源
    最近更新 更多