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 66 67 68 69 70 71 72
|
void HttpPost()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
return;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.cplusplus.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;
}
cout << "Connected.\n";
/*
stream << "POST /.api/api.svc/objects/723aa707-4978-4062-bcc6-67b05783c4ec/comments/add\r\n";
stream << "Host: myurl.com\r\n";
stream << "Accept: WRONG\r\n";
stream << "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n";
stream << "Content-Length: 51\r\n";
stream << "Connection: close\r\n\r\n";
stream << "message=%3Cp%3EHello%3C%2Fp%3E";
*/
char* part1 = "POST /login.do\r\n"
"Host:www.cplusplus.com\r\n"
"Accept: */*\r\n"
"Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"
"Content-Length: 90\r\n"
"Connection: close\r\n\r\n"
"w=login&y=1&to=www.cplusplus.com&l=myusername&p=mypassword";
//_itoa_s(strlen(part2), part1,strlen(part1)-1, 10);
cout << "Bytes sent: " << send(Socket, part1, strlen(part1), 0) << endl;
cout << "Printing outputs..." << endl;
int count = 0;
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
count++;
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
/*
char* packet = part1;
for (int i = strlen(part1) - 1; i < (strlen(part1) - 1)+strlen(part2)-1; i++)
{
packet[i] = part2[i - (strlen(part1))-1];
}
*/
closesocket(Socket);
WSACleanup();
cout << "done." << endl;
}
|