c++ - 处理 HTTP POST 请求/响应

标签 c++ sockets http unix

我一直在尝试向 facebook 发送 HTTP POST 请求但没有成功,我从服务器收到了这个响应:

HTTP/1.1 400 Bad Request Content-Type: text/html; charset=utf-8 Date: Sat, 10 Dec 2016 21:28:17 GMT Connection: close Content-Length: 2959

Facebook |错误

Sorry, something went wrong We're working on it and we'll get it fixed as soon as we can

我的代码

#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <fstream>
using namespace std;

int main()
{
    int s, error;
    struct sockaddr_in addr;


        s = socket(AF_INET, SOCK_STREAM, 0);
    if(s <0)
    {
        cout<<"Error 01: creating socket failed!\n";
        close(s);
        return 1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    inet_aton("31.13.90.36",&addr.sin_addr);

    error = connect(s,(sockaddr*)&addr,sizeof(addr));
    if(error!=0)
    {
        cout<<"Error 02: conecting to server failed!\n";
        close(s);
        return 1;
    }

     const int msgSize = 1042;
     char msg[] = "POST /login.php?login_attempt=1 \r\n"
                     "HTTP/1.1\r\n"
                     "HOST: facebook.com\r\n\r\n"
                     "Content-type: application/x-www-form-urlencoded\r\n"
                     "Content-Length: 41\r\n"
                     "email=lel@gmail.com&pass=test123&submit=1\r\n" ;







     char answ[1042];

    //cin.getline(&msg[0],256);

    send(s,msg,strlen(msg),0);


    ssize_t len;
    while((len = recv(s, msg, strlen(msg), 0)) > 0)
    {
        cout.write(msg, len);

        std::cout << std::flush;
    }
    if(len < 0)
    {
        cout << "error";
    }
    close(s);  

}

我做错了什么?

最佳答案

您的消息中有几个错误。这是您根据代码发送的内容:

1  POST /login.php?login_attempt=1 \r\n
2  HTTP/1.1\r\n 
3  HOST: facebook.com\r\n\r\n
4  Content-type: application/x-www-form-urlencoded\r\n
5  Content-Length: 41\r\n
6  email=lel@gmail.com&pass=test123&submit=1\r\n

应该是这样的:

1  POST /login.php?login_attempt=1 HTTP/1.1\r\n
2  HOST: facebook.com\r\n
3  Content-type: application/x-www-form-urlencoded\r\n
4  Content-Length: 41\r\n
5  \r\n
6  email=lel@gmail.com&pass=test123&submit=1

详细说明:

  • 第 1 行和第 2 行应该是单行,即“方法路径 HTTP 版本”
  • 第 3 行不应包含多个 \r\n
  • 空行 \r\n 应该在所有 HTTP header 之后(新行 5)
  • 第 6 行的正文应仅包含内容长度所涵盖的数据。您在那里设置的 41 个字节不包括您发送的 \r\n

除此之外,您没有正确解析响应,而是期望服务器在响应完成后关闭连接。由于您使用的是 HTTP/1.1,因此您隐含地使用了持久连接(HTTP 保持事件状态),因此服务器实际上可能会在同一个 TCP 连接中等待更多请求,而不是立即关闭连接。

我真的建议你研究standard of HTTP而不是猜测协议(protocol)可能如何工作。

关于c++ - 处理 HTTP POST 请求/响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41080568/

相关文章:

C++无法在构造函数中使用模板化参数转换类

python - 从另一个线程中止 zeromq recv() 或 poll() - 立即且无需等待超时

sockets - 套接字连接没有明显的原因被关闭

Java 小程序 : Socket fails to connect

angular - 在 Angular 4/Express 中访问响应状态代码

c++ - 二维插值

c++ - NVCC,与 -Xcompiler 的奇怪交互

c - 具有 htonl 和 ntohl 功能的 pdp endian

rest - 根据 HTTP DELETE 查看幂等性的正确方法是什么?

angular - .map 运算符后打印的 NaN 值