-2

出于某种原因,我不知道 POST 请求不起作用。我很确定问题出在 POST string"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
4

1 回答 1

0

您可以使用 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&file=funpic&name=rosh").size()并且查询必须放在请求字符串的末尾,在我的情况下是之后Connection: close\r\n\r\n

例如我之前的查询,我想将 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

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

于 2019-11-23T07:14:13.270 回答