Winsock2 登录窗体 c++

Winsock2 login Form c++

所以我已经做了好几天了,我正在尝试使用 C++ 通过 winsock 连接到我自己的网站。 通常我通过 google 找到我需要的一切,但我似乎无法弄清楚。

char *sendbuf = "POST / HTTP/1.1\r\n"
    "Host: sn.theskatenetwork.com\r\n"
    "Accept: text/html, application/xhtml+xml\r\n"
    "Content-Type: multipart/form-data; boundary=---------------------------7dd37b37e06e2\r\n"
    "Content-Length: sizeofbody\r\n"
    "Accept-Encoding: gzip, deflat\r\n"
    "Keep-Alive: 30\r\n"
    "Connection: Keep-Alive\r\n"
    "\r\n"
    "---------------------------7dd37b37e06e2\r\n"
    "Content-Disposition: form-data; name=\"ID\"; text=\"testing\"\r\n"
    "-----------------------------7dd37b37e06e2\r\n"
    "---------------------------7dd37b37e06e2\r\n"
    "Content-Disposition: form-data; name=\"Password\"; text=\"testing\"\r\n"
    "-----------------------------7dd37b37e06e2\r\n"
    "\r\n";

我不太确定我是否发送正确的输入,因为我所看到的一切都只包括发送文件。 我也不知道如何检查 cookie 以查看我是否已登录。我假设为了做到这一点,我必须再次发送作为 get,但这就是我所知道的。 似乎大多数人只使用 vb 来编写 winsock,但现在这不是一个选项。

它不起作用的原因是因为您的 MIME body 数据格式不正确:

  1. 您没有为每个 MIME 部分之间的边界正确使用破折号。如果您摆脱(或至少减少)边界中的破折号,则更容易看到。

  2. 每个 MIME 部分中的文本值格式不正确。 Content-Disposition header 没有 text 属性。需要把正文移动到MIME部分的body,需要用空行把MIME部分的header和body隔开,类似于主HTTP报文的隔开headers 和 body.

试试这个:

char *sendbuf = "POST / HTTP/1.1\r\n"
    "Host: sn.theskatenetwork.com\r\n"
    "Accept: text/html, application/xhtml+xml\r\n"
    "Content-Type: multipart/form-data; boundary=7dd37b37e06e2\r\n"
    "Content-Length: 167\r\n"
    "Accept-Encoding: gzip, deflate\r\n"
    "Keep-Alive: 30\r\n"
    "Connection: Keep-Alive\r\n"
    "\r\n"
    "--7dd37b37e06e2\r\n"
    "Content-Disposition: form-data; name=\"ID\"\r\n"
    "\r\n"
    "testing\r\n"
    "--7dd37b37e06e2\r\n"
    "Content-Disposition: form-data; name=\"Password\"\r\n"
    "\r\n"
    "testing\r\n"
    "--7dd37b37e06e2--\r\n";

或更多 C++,例如:

std::string host = "sn.theskatenetwork.com";
std::string boundary = "7dd37b37e06e2";
std::string id = "testing";
std::string psw = "testing";

std::ostringstream oss;
oss << "--" << boundary << "\r\n"
       "Content-Disposition: form-data; name=\"ID\"\r\n"
       "\r\n"
       << id << "\r\n"
       "--" << boundary << "\r\n"
       "Content-Disposition: form-data; name=\"Password\"\r\n"
       "\r\n"
       << psw << "\r\n"
       "--" << boundary << "--\r\n";

std::string senddata = oss.str();

oss.str("");
oss.clear();

oss << "POST / HTTP/1.1\r\n"
       "Host: " << host << "\r\n"
       "Accept: text/html, application/xhtml+xml\r\n"
       "Content-Type: multipart/form-data; boundary=" << boundary << "\r\n"
       "Content-Length: " << senddata.size() << "\r\n"
       "Accept-Encoding: gzip, deflate\r\n"
       "Keep-Alive: 30\r\n"
       "Connection: Keep-Alive\r\n"
       "\r\n"
       << senddata;

std::string sendbuf = oss.str();

// use sendbuf.c_str() and sendbuf.size() as needed...

阅读 RFC 2045, 2046, 2047, 2048, and 2049 以获得官方 MIME 规范。

阅读 W3C 的 HTML 规范(HTML4 and HTML5), as well as RFC 2388,了解如何格式化 multipart/form-data 数据。

至于 cookie,它们会在 HTTP 响应中发送给您,您会在后续请求中将它们发回给同一个 server/path。阅读 RFC 6265 以了解官方 cookie 规范。