Trying an HTTP POST req in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Trying an HTTP POST req in C++

  1. #1
    Join Date
    Apr 2010
    Posts
    19

    Question Trying an HTTP POST req in C++

    Hi everyone,

    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

    <Payload>...stuff here...</Payload>

    ------------------------------------------------------------------------------------------

    For those of you who are curious, here's the code:

    ------------------------------------------------------------------------------------------


    void BuildPayload(string* PtrPayloadBuffer, string Stuff)
    {
    string Part1="<Payload>";
    string Part2="</Payload>";

    (*PtrPayloadBuffer).clear();
    (*PtrPayloadBuffer).append(Part1);
    (*PtrPayloadBuffer).append(Stuff);
    (*PtrPayloadBuffer).append(Part2);
    }


    void BuildHeader(string* PtrHeaderBuffer, string* PtrPayloadBuffer, sockaddr_in RemoteServer)
    {
    string Part1="POST /login.jsp HTTP/1.1\nHost: ";
    string Part2="\nContent-Length: ";
    string Part3="\nContent-Type: text/html; charset=ISO-8859-4\r\n\r\n";
    char * MsgSize=(char*) malloc (sizeof(char) * 5); memset(MsgSize,0,5);
    int DontCare;
    DontCare=sprintf(MsgSize, "%d", PtrPayloadBuffer->size());

    (*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

    BuildPayload(PtrPayloadBuffer, AppName, L4Proto, L4Port);
    BuildHeader(PtrHeaderBuffer, PtrPayloadBuffer, RemoteServer);
    HeaderBuffer.append(PayloadBuffer);

    // Write string HeaderBuffer to socket, then close

    return 1;
    }


    ------------------------------------------------------------------------------------------

  2. #2
    Join Date
    Jul 2010
    Posts
    10

    Re: Trying an HTTP POST req in C++

    what kind of sockets are you using

    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

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Trying an HTTP POST req in C++

    Just out of curiosity, why are you connecting via sockets directly? libcurl has been the standard way to do web communication for years.

    And god only help you if you are trying to do socket communication with Windows. UNIX sockets will not talk to windows sockets easily.

  4. #4
    Join Date
    May 2008
    Posts
    38

    Re: Trying an HTTP POST req in C++

    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 ...

  5. #5
    Join Date
    Apr 2010
    Posts
    19

    Re: Trying an HTTP POST req in C++

    Hi y'all,

    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;


    struct sockaddr_in RemoteServer;
    struct hostent *ServerIP;

    Socket1 = socket(AF_INET, SOCK_STREAM, 0);
    if(Socket1 < 0) { Error("ERROR -- Opening Socket Error!\n"); }

    ServerIP = gethostbyname(argv[1]);
    if(ServerIP == NULL) { Error("ERROR -- Host Not Found!\n"); }
    memset( &RemoteServer, 0, sizeof(RemoteServer) );
    RemoteServer.sin_family = AF_INET;
    bcopy( (char *)ServerIP->h_addr, (char *) &RemoteServer.sin_addr.s_addr, ServerIP -> h_length);
    RemoteServer.sin_port = htons(atoi(argv[2]));

    // 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"); }

    BuildPayload(PtrPayloadBuffer, AppName, L4Proto, L4Port);
    BuildHeader(PtrHeaderBuffer, PtrPayloadBuffer, RemoteServer);

    HeaderBuffer.append(PayloadBuffer);

    cout<<"About to send: \""<<HeaderBuffer<<"\" (Size: "<<strlen(HeaderBuffer.c_str())<<")\n";

    // We write the buffer's contents to the socket
    Write1=write( Socket1, HeaderBuffer.c_str(), strlen(HeaderBuffer.c_str()) );

    if(Write1<0) { Error("ERROR -- Cannot write to socket!\n"); }
    else cout<<"Sent! (Write1 is: "<<Write1<<")\n";

    return ReturnCode;
    }
    -----------------------------------------------------------------------------------------------------------------------

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Trying an HTTP POST req in C++

    libcurl doesn't require that you be root as far as I know.

    Besides, if you work for them, and you need something, they should give it to you, especially if it's free.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center