Thread: HTTP POST Request in C++ - header problems

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Question HTTP POST Request in C++ - header problems

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

    Code:
    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:

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


    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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You failed to tell us what is the issue, so I'll throw a few things out there.

    Code:
    POST /login.jsp HTTP/1.1\nHost:
    \r\n is the separator.

    Why are you passing pointers around in C++?
    Why are you using malloc in C++?
    Why aren't you using stringstreams?

    Quick Example:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    using namespace std;
    
    void BuildHeader(stringstream &hdr, const string &payload, const sockaddr_in *remoteServer)
    {
        char buf[16] = { 0 };
        hdr << "POST /login.jsp HTTP/1.1\r\n";
        hdr << "Host: " << inet_ntop(AF_INET,
                                     (const void *)&remoteServer->sin_addr,
                                     buf,
                                     sizeof(buf)) << "\r\n";
        hdr << "Content-Length: " << payload.size() << "\r\n";
        hdr << "Content-Type: text/html; charset=ISO-8859-4\r\n\r\n";
    }
    
    int main(void)
    {
        sockaddr_in sa;
        inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr));
        stringstream ss;
        BuildHeader(ss, "<Payload></Payload>", &sa);
    
        cout << ss.str();
        return 0;
    }
    yields

    Code:
    ./posttest 
    POST /login.jsp HTTP/1.1
    Host: 127.0.0.1
    Content-Length: 19
    Content-Type: text/html; charset=ISO-8859-4
    Last edited by rags_to_riches; 07-20-2010 at 04:56 PM. Reason: Added example code.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    Question

    Hey there, rags_to_riches,

    Thanks for the feedback and the example code! I'm a largely self-taught programmer, so if I pick up bad habits, I don't realize it until someone wiser and more experienced (like you) points them out to me.

    I think you guessed at what my issue was. Basically, I'm writing this program to automatically post material on a website. When I execute the code, the program successfully sends my packet out across the network, yet I never see the intended update on the website. Hence the reason for this posting.

    You asked a few questions ("Why are you using malloc?" etc.) and the short answer to all of them is I picked them up from another C++ programmer years ago and now they are in my standard "bag of tricks." But you did ask one question which threw me - "Why are you passing pointers in C++?" I always pass pointers in C++, I thought it was the most sophicated way to share variables between functions. Is there something wrong with it? Or is there something wrong with what I'm doing in this program?

    Many thanks!
    -P

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    6
    hi phummon,

    I got curious by your last question. Perhaps it may create an interesting discussion. My point of view is that pointers are powerful features of C++ so it's not reasonable to say there is something wrong using them. But the important point can be summarized with the quote "With great power comes great responsibility."


    You may need to be careful with such things like unintended operations on the system memory, memory leak and the like.

    Unless you are working in an environment like .NET where your code is managed by a "super man" named .NETs run time library which guards your memory from alien attacks, the only choice you got is to use things wisely.
    Last edited by iC++; 07-21-2010 at 09:32 AM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. http head request
    By nishanthaMe in forum C Programming
    Replies: 5
    Last Post: 10-01-2007, 11:57 PM
  3. HTTP Post Question
    By penney in forum C# Programming
    Replies: 2
    Last Post: 06-07-2004, 09:26 AM
  4. Http Get and Post commands
    By Karlo in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2003, 10:30 AM
  5. I Need A Example For Http Post
    By rjhome in forum C Programming
    Replies: 5
    Last Post: 02-17-2002, 10:49 AM

Tags for this Thread