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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sending HTTP Post via TCP/IP

Sending HTTP Post via TCP/IP

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasecomquestion
7 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.
  • D Offline
    D Offline
    Dustin Henry
    wrote on last edited by
    #1

    So, I'm fairly familiar with socket programming, but I have never before tried to send HTTP arguments to a web page before using WinSock. I'm trying to duplicate the data in the following URL: http://www.thewebsite.com/sub/database?action=authenticate&username=test&password=test First, I am opening a socket to www.thewebsite.com on port 80. Then, I am sending the following: std::string message = "POST /sub/database?action=authenticate&username=test&password=test"; send(m_hConnection, message.c_str(), message.length(), 0); The problem is, I don't receive a response making me assume the format is incorrect since pasting the URL in a browser does return a response. Now, I'm kind of just guessing on the format to send the data in as I can't seem to find a good description online that doesn't involve a 3rd party library. I have the following code in another thread in attempt to receive this data: char recvbuf[MAX_BUFFER_LENGTH]; int result = recv(m_hConnection, recvbuf, MAX_BUFFER_LENGTH, 0) Any help is greatly appreciated. Dustin

    D M M 3 Replies Last reply
    0
    • D Dustin Henry

      So, I'm fairly familiar with socket programming, but I have never before tried to send HTTP arguments to a web page before using WinSock. I'm trying to duplicate the data in the following URL: http://www.thewebsite.com/sub/database?action=authenticate&username=test&password=test First, I am opening a socket to www.thewebsite.com on port 80. Then, I am sending the following: std::string message = "POST /sub/database?action=authenticate&username=test&password=test"; send(m_hConnection, message.c_str(), message.length(), 0); The problem is, I don't receive a response making me assume the format is incorrect since pasting the URL in a browser does return a response. Now, I'm kind of just guessing on the format to send the data in as I can't seem to find a good description online that doesn't involve a 3rd party library. I have the following code in another thread in attempt to receive this data: char recvbuf[MAX_BUFFER_LENGTH]; int result = recv(m_hConnection, recvbuf, MAX_BUFFER_LENGTH, 0) Any help is greatly appreciated. Dustin

      D Offline
      D Offline
      Dustin Henry
      wrote on last edited by
      #2

      Found 2 of my problems. First, it appears there is a problem receiving on the socket from a different thread that the one that created it. Can anyone confirm this and give me a possible solution? Second, I added "HTTP/1.0\n" after POST in my message, I receive a response on a recv line directly after the send (not in the threaded method), but now I'm getting a response of: HTTP/1.1 400 Bad Request Content-Type: text/html Date: Mon, 13 Sep 2010 21:35:24 GMT Connection: close Content-Length: 34 <h1>Bad Request (Invalid URL)</h1> Any ideas?

      M 1 Reply Last reply
      0
      • D Dustin Henry

        So, I'm fairly familiar with socket programming, but I have never before tried to send HTTP arguments to a web page before using WinSock. I'm trying to duplicate the data in the following URL: http://www.thewebsite.com/sub/database?action=authenticate&username=test&password=test First, I am opening a socket to www.thewebsite.com on port 80. Then, I am sending the following: std::string message = "POST /sub/database?action=authenticate&username=test&password=test"; send(m_hConnection, message.c_str(), message.length(), 0); The problem is, I don't receive a response making me assume the format is incorrect since pasting the URL in a browser does return a response. Now, I'm kind of just guessing on the format to send the data in as I can't seem to find a good description online that doesn't involve a 3rd party library. I have the following code in another thread in attempt to receive this data: char recvbuf[MAX_BUFFER_LENGTH]; int result = recv(m_hConnection, recvbuf, MAX_BUFFER_LENGTH, 0) Any help is greatly appreciated. Dustin

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        Correct HTTP protocol[^] requires sending more than just the request line. If the service responds to pasting the URL to a browser, you must use the GET method instead of POST. The form data[^] is appended to the URL as name=value pairs separated by "&" characters.

        D 1 Reply Last reply
        0
        • M markkuk

          Correct HTTP protocol[^] requires sending more than just the request line. If the service responds to pasting the URL to a browser, you must use the GET method instead of POST. The form data[^] is appended to the URL as name=value pairs separated by "&" characters.

          D Offline
          D Offline
          Dustin Henry
          wrote on last edited by
          #4

          Thanks for the reply, but linking to a 176 page document is not helpful in answering my simple question. Also, if you would look at the syntax I used I do understand the name=value pairs quite well. If you look at the reply I posted to my own post, I added the correct HTTP/1.0 header and now receive a response, but not a good one. Also, using GET instead of post does not seem to make a difference. Thanks, Dustin

          1 Reply Last reply
          0
          • D Dustin Henry

            So, I'm fairly familiar with socket programming, but I have never before tried to send HTTP arguments to a web page before using WinSock. I'm trying to duplicate the data in the following URL: http://www.thewebsite.com/sub/database?action=authenticate&username=test&password=test First, I am opening a socket to www.thewebsite.com on port 80. Then, I am sending the following: std::string message = "POST /sub/database?action=authenticate&username=test&password=test"; send(m_hConnection, message.c_str(), message.length(), 0); The problem is, I don't receive a response making me assume the format is incorrect since pasting the URL in a browser does return a response. Now, I'm kind of just guessing on the format to send the data in as I can't seem to find a good description online that doesn't involve a 3rd party library. I have the following code in another thread in attempt to receive this data: char recvbuf[MAX_BUFFER_LENGTH]; int result = recv(m_hConnection, recvbuf, MAX_BUFFER_LENGTH, 0) Any help is greatly appreciated. Dustin

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            Dustin Henry wrote:

            I'm trying to duplicate the data in the following URL: http://www.thewebsite.com/sub/database?action=authenticate&username=test&password=test

            This would be a GET request not a POST, translated into HTTP 1.1:

            GET /sub/database?action=authenticate&username=test&password=test HTTP/1.1\r\n
            Host: www.thewebsite.com\r\n\r\n

            Does this help? :) /M

            Chat in Europe :java: Now with 24% more Twitter

            D 1 Reply Last reply
            0
            • D Dustin Henry

              Found 2 of my problems. First, it appears there is a problem receiving on the socket from a different thread that the one that created it. Can anyone confirm this and give me a possible solution? Second, I added "HTTP/1.0\n" after POST in my message, I receive a response on a recv line directly after the send (not in the threaded method), but now I'm getting a response of: HTTP/1.1 400 Bad Request Content-Type: text/html Date: Mon, 13 Sep 2010 21:35:24 GMT Connection: close Content-Length: 34 <h1>Bad Request (Invalid URL)</h1> Any ideas?

              M Offline
              M Offline
              Moak
              wrote on last edited by
              #6

              Dustin Henry wrote:

              Any ideas?

              Looks like a violation of the HTTP RFC, when client sends a HTTP 1.0 request the server must not answer with a HTTP 1.1 response as in your example. Well, try to send a HTTP 1.1 request as shown in my post below (including a Host header line), perhaps the server understand only HTTP 1.1. What kind of web server software are you using? /M

              Chat in Europe :java: Now with 24% more Twitter

              1 Reply Last reply
              0
              • M Moak

                Dustin Henry wrote:

                I'm trying to duplicate the data in the following URL: http://www.thewebsite.com/sub/database?action=authenticate&username=test&password=test

                This would be a GET request not a POST, translated into HTTP 1.1:

                GET /sub/database?action=authenticate&username=test&password=test HTTP/1.1\r\n
                Host: www.thewebsite.com\r\n\r\n

                Does this help? :) /M

                Chat in Europe :java: Now with 24% more Twitter

                D Offline
                D Offline
                Dustin Henry
                wrote on last edited by
                #7

                Thank you, this helps a lot. Along with the GET, adding the correct formatting you've shown at the end of my request makes it work. Thanks for the help :) Dustin

                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