I'm a moderately experienced C++ programmer who's trying to do a little socket work. Don't ask why, but I'm to write a program which, when activated, sends an HTTP POST request to a remote end server. The socket part of the work is done and successfully tested; all I have to do is get the HTTP header exactly right. Unfortunately, the resources I've consulted online aren't that helpful and I can't tell where I'm screwing up the header. Can someone quickly spot the error or recommend a solid resource which will really detail what I want to do?
Here's the HTTP request I'm sending to the remote server:
------------------------------------------------------------------------------------------
POST /login.jsp HTTP/1.1
Host: 135.51.161.73
Content-Length: 170
Content-Type: text/html; charset=ISO-8859-4
(*PtrHeaderBuffer).clear();
(*PtrHeaderBuffer).append(Part1);
(*PtrHeaderBuffer).append("10.1.1.100"); // don't know how to encode IP Addr as string
(*PtrHeaderBuffer).append(Part2);
(*PtrHeaderBuffer).append(MsgSize);
(*PtrHeaderBuffer).append(Part3);
}
int main(int argc, char * argv[])
{
// Build socket to remote server 10.1.1.100
I tried for years to do HTTP with winsock but for some reason, servers just won't talk to you
(despite having successfully performed HTTP requests in 3 other languages :P)
If you're using winsock, you'll probably have to just give up
If you do figure it out though, please let me know what the trick is ;-)
For a project I'm currently working on, I ended up using libcurl instead
It's pretty sleek and easy to use
I've tried the same thing before, if it is on windows then you can use WinInet to create the HTTP requests and that for you. I've had slightly more sucess with that ...
Thanks for the comments and feedback. To reply to some of the specific things you said...
The raw code for my sockets is below. It compiles and runs just fine. I have no objection to using a library like curl for the heavy lifting. However, I'm programming on a department machine on which I am not the admin, so adding a new library isn't really an option for me if it involves any system-altering changes.
For those of you who are curious, here's the code I didn't include last time:
-----------------------------------------------------------------------------------------------------------------------
int main(int argc, char * argv[])
{
int ReturnCode=1;
int Socket1, Connect1, Write1, Close1;
string PayloadBuffer; string* PtrPayloadBuffer=&PayloadBuffer;
string HeaderBuffer; string* PtrHeaderBuffer=&HeaderBuffer;
string AppName="124_PDH_Custom";
string L4Proto="TCP";
string InputFile; string* PtrInputFile = &InputFile;
int L4Port=3333;
// Having already built the socket, we now connect to the end server
Connect1=connect( Socket1, (struct sockaddr*) &RemoteServer, sizeof(RemoteServer));
if(Connect1 < 0) { Error("ERROR -- Could not connect to server!\n"); }
Bookmarks