【问题标题】:C++ Schannel POST 400 Error Code Bad RequestC++ Schannel POST 400 错误代码错误请求
【发布时间】:2020-03-18 02:20:26
【问题描述】:

由于某种原因,我不知道 POST 请求不起作用。我很确定问题出在 POST 字符串 "POST /profile.php HTTP/1.1\r\nid=2323\r\nContent-Length: \r\n\r\n" 上。 GET 请求正常工作,但 POST 请求不能正常工作。

我使用 TLS VER 1.0 通过端口 443 连接到服务器

这是我从服务器获得的错误代码。编辑:我正在尝试编辑这篇文章以修复它,它告诉我我的帖子大部分是代码,但我希望不再是。

85 bytes of data sent

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=utf-8
Date: Fri, 22 Nov 2019 06:56:20 GMT
Connection: close
Content-Length: 2959

<!DOCTYPE html>
<html lang="en" id="facebook">
  <head>
    <title>Facebook | Error</title>
    <meta charset="utf-8">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="cache-control" content="no-store">
    <meta http-equiv="cache-control" content="max-age=0">
    <meta http-equiv="expires" content="-1">
    <meta http-equiv="pragma" content="no-cache">
    <meta name="robots" content="noindex,nofollow">
    <style>
      html, body {
        color: #141823;
        background-color: #e9eaed;
        font-family: Helvetica, Lucida Grande, Arial,
                     Tahoma, Verdana, sans-serif;
        margin: 0;
        padding: 0;
        text-align: center;
      }

      #header {
        height: 30px;
        padding-bottom: 10px;
        padding-top: 10px;
        text-align: center;
      }

      #icon {
        width: 30px;
      }

      h1 {
        font-size: 18px;
      }

      p {
        font-size: 13px;
      }

      #footer {
        border-top: 1px solid #ddd;
        color: #9197a3;
        font-size: 12px;
        padding: 5px 8px 6px 0;
      }
    </style>
  </head>
  <body>
    <div id="header">
      <a href="//www.facebook.com/">
        <img id="icon" src="//static.facebook.com/images/logos/facebook_2x.png" />
      </a>
    </div>
    <div id="core">
      <h1 id="sorry">Sorry, something went wrong.</h1>
      <p id="promise">
        We're working on it and we'll get it fixed as soon as we can.
      </p>
      <p id="back-link">
        <a id="back" href="//www.facebook.com/">Go Back</a>
      </p>
      <div id="footer">
        Facebook
        <span id="copyright">
          &copy; 2019
        </span>
        <span id="help-link">
          &#183;
          <a id="help" href="//www.facebook.com/help/">Help Center</a>
        </span>
      </div>
    </div>
    <script>
      document.getElementById('back').onclick = function() {
        if (history.length > 1) {
          history.back();
          return false;
        }
      };

      // Adjust the display based on the window size
      if (window.innerHeight < 80 || window.innerWidth < 80) {
        // Blank if window is too small
        document.body.style.display = 'none';
      };
      if (window.innerWidth < 200 || window.innerHeight < 150) {
        document.getElementById('back-link').style.display = 'none';
        document.getElementById('help-link').style.display = 'none';
      };
      if (window.innerWidth < 200) {
        document.getElementById('sorry').style.fontSize = '16px';
      };
      if (window.innerWidth < 150) {
        document.getElementById('promise').style.display = 'none';
      };
      if (window.innerHeight < 150) {
        document.getElementById('sorry').style.margin = '4px 0 0 0';
        document.getElementById('sorry').style.fontSize = '14px';
        document.getElementById('promise').style.display = 'none';
      };
    </script>
  </body>
</html>


3215 bytes received

【问题讨论】:

  • 为什么这个问题用 C++ 标记?
  • 好吧,我可以将它标记为 C 或 C++,但由于我将 sspi schannel 与 C++ 一起使用,因此我将其标记为 C++
  • 没有 C++ 代码。 C++ 开发人员如何帮助您?发布 C++ 代码或删除标签。
  • 我不明白你背后的逻辑,“如果没有c++代码,那就不是c++”。为了让你在 C++ 中发送一个 POST 请求,它需要你正确地格式化它,就像在正确的新行和所有那些疯狂的东西中一样。其他编程语言不一样。几乎所有在 c++ 中使用 winsock 编程的人都明白,这个 "POST /profile.php HTTP/1.1\r\nid=2323\r\nContent-Length: \r\n\r\n" 仅适用于 C++ 和类似语言.
  • 您用 C++ 标记了这个问题,以接触有 C++ 经验的人,但 C++ 经验对这个问题没有帮助。您还可以标记您的操作系统或显示器的制造商。当然,它在某种程度上是相关的,但它无济于事。使用 C++ 标准、C++ 库或 C++ 编译器的知识无法回答这个问题。它是特定于 Winsock 的。

标签: c++ network-programming winsock sspi schannel


【解决方案1】:

您可以使用 GET 请求代替发出 POST 请求并执行此操作

"GET /profile.php?id=2323 HTTP/1.1\r\nConnection: close\r\n\r\n"

这主要取决于您要连接的服务器,所以如果这不起作用,请尝试使用 HTTP v1.0

"GET /profile.php?id=2323 HTTP/1.0\r\nConnection: close\r\n\r\n"

如果这仍然不起作用,您可能必须指定主机,主要是在您尝试从类似于https://en30.picwebsite.com 的网站获取图像时

"GET /profile.php?id=2323 HTTP/1.1\r\nHost: en30.picwebsite.com\r\nConnection: close\r\n\r\n"

同时尝试 HTTP v1.0 和 v1.1

如果一切都不起作用,请确保您使用 SSL/TLS 进行连接(如果它是安全网站)。如果是,请尝试主页简单的 GET 请求 "GET / HTTP/1.1\r\nConnection: close\r\n\r\n" 以及 HTTP v1.0 和 v1.1

如果这也不起作用,那么可能是您的 Socket/TLS 实现或服务器存在问题。

编辑:

我终于想出了如何发出正确的 POST 请求。您必须始终在 POST 请求中指定内容长度,并且内容长度应该是查询的大小,例如 - "std::string("id=2323&amp;file=funpic&amp;name=rosh").size() 并且查询必须位于请求字符串的末尾,在我的情况下是在 @ 之后987654327@

例如我之前的查询,我想将 id 更改为 2323 /profile.php?id=2323,id 这样做 -

std::string query = "id=2323";
std::string request = "POST /profile.php HTTP/1.1\r\nContent-Length: " + 
std::to_string(query.size()) + "\r\nConnection: close\r\n\r\n" + query;
send(sock, request.c_str(), request.size(), 0);

您也可以在查询后添加\r\n,它仍然可以工作id=2323\r\n

确保仅在完整请求之后添加查询,因为查询应该在网站内容中而不是在标题中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    相关资源
    最近更新 更多