HTTP POST req 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:
------------------------------------------------------------------------------------------

1
2
3
4
5
6
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:

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
}



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


Topic archived. No new replies allowed.