Jul 31, 2014 at 12:58pm UTC
I am trying to perform a POST request, but my code is not working so I'd like to know any alternative methods.
Im looking for an easier method without using sockets.
I.e. WinInet, which I use to make a succesful GET request.
What method should I use? I really hate sockets, I like to keep it simple.
If you are wondering, this is my failed attempt: (its a mess I know)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
void NewHttpPost()
{
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("http://www.totaljerkface.com/" );
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long *)host->h_addr);
cout << "Connecting...\n" ;
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof (SockAddr)) != 0){
cout << "Could not connect" ;
return ;
}
std::ostringstream FormBuffer; // <<< here
char DataType1[] = "login_user_email=" ;
char DataType2[] = "&login_user_pass=" ;
char DataType3[] = "&action=" ;
const char username[] = "sadasd" ;
const char password[] = "asdasdasd" ;
const char action[] = "login" ;
const char FormAction[] = "/login.do" ;
// get: length of the actual content
auto ContentLength = strlen(username) +
strlen(password) +
strlen(action) +
strlen(DataType1) +
strlen(DataType2) +
strlen(DataType3);
// header
FormBuffer << "POST " << FormAction << " HTTP/1.1\n" ;
FormBuffer << "Content-Type: application/x-www-form-urlencoded\n" ;
FormBuffer << "Host: http://www.totaljerkface.com/ \n" ;
FormBuffer << "Content-Length: " << std::to_string(ContentLength) << "\n\n" ;
// actual content
FormBuffer << DataType1 << username;
FormBuffer << DataType2 << password;
FormBuffer << DataType3 << action;
const auto str = FormBuffer.str();
std::cout << str << std::endl;
send(Socket, str.data(), str.size(), NULL);
/*
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
*/
closesocket(Socket);
//WSACleanup();
}
Last edited on Jul 31, 2014 at 12:59pm UTC
Jul 31, 2014 at 4:55pm UTC
Nvm, fixed the code myself.