Sending HTTP Post via TCP/IP
-
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 -
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. DustinFound 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?
-
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. DustinCorrect 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. -
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.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
-
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. DustinDustin 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\nDoes this help? :) /M
-
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?
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 -
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\nDoes this help? :) /M
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