简体   繁体   中英

Winsock2 login Form c++

so i have been at this for days, im trying to connect to my own website through winsock using c++. usually i find everything i need through google but i cannot seem to figure it out.

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";

I am not too sure if i am sending the input right as everything i have seen only consists of sending files. I also cannot figure out how to check cookies to see if i have been logged in. I am assuming that in order to do that I've got to send again as a get but that's about all i know. It seems as if most people only use vb to write winsock but that is not an option right now.

The reason it does not work is because your MIME body data is malformed:

  1. you are not using dashes correctly for the boundaries in between each MIME part. If you get rid of (or at least reduce) the dashes in your boundary, this is easier to see.

  2. your text values inside of each MIME part is not formatted correctly. The Content-Disposition header does not have a text attribute. You need to move the text to the MIME part's body, and you need to separate the MIME part's headers and body with a blank line, similar to the separation of the main HTTP message's headers and body.

Try this instead:

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";

Or more C++ like:

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...

Read RFCs 2045 , 2046 , 2047 , 2048 , and 2049 for the official MIME spec.

Read W3C's HTML specs ( HTML4 and HTML5 ), as well as RFC 2388 , for how to format multipart/form-data data.

As for cookies, they are sent to you in an HTTP response, and you send them back in subsequent requests to the same server/path. Read RFC 6265 for the official cookie spec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM