Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WinSOCK and HTTP POST - how to send/receive data?

WinSOCK and HTTP POST - how to send/receive data?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialphpcomsysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Laursen
    wrote on last edited by
    #1

    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.

    RaviBeeR 1 Reply Last reply
    0
    • P Peter Laursen

      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.

      RaviBeeR Offline
      RaviBeeR Offline
      RaviBee
      wrote on last edited by
      #2

      See this[^] article. If you also want to retrieve information from the webserver's response, see this[^] article. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

      H 1 Reply Last reply
      0
      • RaviBeeR RaviBee

        See this[^] article. If you also want to retrieve information from the webserver's response, see this[^] article. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

        H Offline
        H Offline
        humps
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • H humps

          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.

          P Offline
          P Offline
          Peter Laursen
          wrote on last edited by
          #4

          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

          H 1 Reply Last reply
          0
          • P Peter Laursen

            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

            H Offline
            H Offline
            humps
            wrote on last edited by
            #5

            Hi Peter, Looks like you may have forgotten to account for "Name=" in the Content-Length. Name.length() is actually the length of your content minus the length of the string "Name=". Hope this helps... Thanks, Neil Humphreys.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups