phummon 0 Newbie Poster

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 (briefer than it looks):

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

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;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.