WinSOCK and HTTP POST - how to send/receive data?
-
Hi everyone, I have asked on several programming forums and googled for weeks, but I am unable to find just exactly how to send and receive data from a client to a webserver. What I need is a simple example on how to send a POST request to a webserver using WinSock. (Form data name=YourName&Age=20) This code should (if I understand everything correctly) work, yet it does not. Whenever the server gets back to me, all it gives me is a 0. Here is the simple PHP script I use for testing: Please help, Peter --Code-- #include #include #include #include using namespace std; int main() { WSADATA WSAData; WSAStartup(MAKEWORD(1,1), &WSAData); // Someone told me to use the lowest version of Winsock that satisfied my needs struct hostent* Host; struct sockaddr_in Server; Server.sin_family = AF_INET; Server.sin_port = htons(80); Host = gethostbyname("tdlgames.com"); // One of my websites. Uses the Apachee webserver on a Linux platform. Server.sin_addr = *((struct in_addr*)Host->h_addr); SOCKET MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); connect(MySocket, (struct sockaddr*)&Server, sizeof(struct sockaddr)); string SendInfo("POST /Test.php http/1.1\r\nHost: tdlgames.com\r\nUser-Agent: TestPostMethod/1.0\r\nContent-Type: application-www-form-urlencoded\r\n\r\nName=Peter"); // Eventually, the error might be in the things I send - again, please advise on what to do... int BytesSent = send(MySocket, SendInfo.c_str(), SendInfo.length(), 0); char Buffer[1024]; while (recv(MySocket, Buffer, 1023, 0)) cout << Buffer << endl; closesocket(MySocket); WSACleanup(); system("pause"); } --- Thanks for reading.
-
Hi everyone, I have asked on several programming forums and googled for weeks, but I am unable to find just exactly how to send and receive data from a client to a webserver. What I need is a simple example on how to send a POST request to a webserver using WinSock. (Form data name=YourName&Age=20) This code should (if I understand everything correctly) work, yet it does not. Whenever the server gets back to me, all it gives me is a 0. Here is the simple PHP script I use for testing: Please help, Peter --Code-- #include #include #include #include using namespace std; int main() { WSADATA WSAData; WSAStartup(MAKEWORD(1,1), &WSAData); // Someone told me to use the lowest version of Winsock that satisfied my needs struct hostent* Host; struct sockaddr_in Server; Server.sin_family = AF_INET; Server.sin_port = htons(80); Host = gethostbyname("tdlgames.com"); // One of my websites. Uses the Apachee webserver on a Linux platform. Server.sin_addr = *((struct in_addr*)Host->h_addr); SOCKET MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); connect(MySocket, (struct sockaddr*)&Server, sizeof(struct sockaddr)); string SendInfo("POST /Test.php http/1.1\r\nHost: tdlgames.com\r\nUser-Agent: TestPostMethod/1.0\r\nContent-Type: application-www-form-urlencoded\r\n\r\nName=Peter"); // Eventually, the error might be in the things I send - again, please advise on what to do... int BytesSent = send(MySocket, SendInfo.c_str(), SendInfo.length(), 0); char Buffer[1024]; while (recv(MySocket, Buffer, 1023, 0)) cout << Buffer << endl; closesocket(MySocket); WSACleanup(); system("pause"); } --- Thanks for reading.
-
Dude I think you are just missing the Content-Length specification in your POST request header. This is needed so the server knows when the content-data ends (\r\n\r\n is only the terminator for the header). Try sending something like this to the server: POST /logon HTTP/1.1 Host: peer200:6002 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: https://peer200:6002/logon Content-Type: application/x-www-form-urlencoded Content-Length: 54 USERNAME=Administrator&PASSWORD=xxxxxxxx&SUBMIT=SUBMIT ...here the length of the content data (USERNAME ....SUBMIT is exactly 54 bytes). Hope this helps ! Thanks, Neil Humphreys.
-
Dude I think you are just missing the Content-Length specification in your POST request header. This is needed so the server knows when the content-data ends (\r\n\r\n is only the terminator for the header). Try sending something like this to the server: POST /logon HTTP/1.1 Host: peer200:6002 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: https://peer200:6002/logon Content-Type: application/x-www-form-urlencoded Content-Length: 54 USERNAME=Administrator&PASSWORD=xxxxxxxx&SUBMIT=SUBMIT ...here the length of the content data (USERNAME ....SUBMIT is exactly 54 bytes). Hope this helps ! Thanks, Neil Humphreys.
Thanks for the replies, all of you. Neil, your headers worked beautifully if I sent them as-is. However, whenever I try to do something complicated like asking the user to type in his name and then change the Content-Length parameter accordingly, the server sends back a 0... Here is the new snippet I have been adding: connect(MySocket, (struct sockaddr*)&Server, sizeof(struct sockaddr)); string Send("POST /Test.php HTTP/1.1\r\nHost: tdlgames.com:80\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded\r\n"); cout << "Enter your name: "; string Name; getline(cin, Name); char Content[100]; sprintf(Content, "Content-Length: %d\r\n\r\n", Name.length()); Send += Content; Send += "Name="; Send += Name; send(MySocket, Send.c_str(), Send.length(), 0); ... The receive code has not changed. Please help me a bit more - I am grateful for all the answers. Thank you, Peter. Visit http://www.tdlsoftware.com
-
Thanks for the replies, all of you. Neil, your headers worked beautifully if I sent them as-is. However, whenever I try to do something complicated like asking the user to type in his name and then change the Content-Length parameter accordingly, the server sends back a 0... Here is the new snippet I have been adding: connect(MySocket, (struct sockaddr*)&Server, sizeof(struct sockaddr)); string Send("POST /Test.php HTTP/1.1\r\nHost: tdlgames.com:80\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded\r\n"); cout << "Enter your name: "; string Name; getline(cin, Name); char Content[100]; sprintf(Content, "Content-Length: %d\r\n\r\n", Name.length()); Send += Content; Send += "Name="; Send += Name; send(MySocket, Send.c_str(), Send.length(), 0); ... The receive code has not changed. Please help me a bit more - I am grateful for all the answers. Thank you, Peter. Visit http://www.tdlsoftware.com