title: How can I retrieve the http body?
body: <p>I have problem with my code. When i'm sending response in c# server to a c++ client i end up with weird response.</p>
  
  <p>Note: <code>DownloadData</code> method is just receiving data from the server, nothing else.</p>
  
  <p>C# Response Handle:</p>
  
  <pre><code>void HandleHttpRequest(object client)
  {
      HttpListenerContext cc = (HttpListenerContext)client;
      cc.Response.Headers.Add("Test: Foo");
      cc.Response.OutputStream.Write(Encoding.Default.GetBytes("Foo"), 0, Encoding.Default.GetBytes("Foo").Length);
      cc.Response.Close();
  }
  </code></pre>
  
  <p>C++ Client</p>
  
  <pre><code>void DoHttpRead()
  {
      char* downloadedHttpBodyAndHeaders = DownloadData("127.0.0.1:49578");
      cout &lt;&lt; string(downloadedHttpBodyAndHeaders) &lt;&lt; endl;
  }
  </code></pre>
  
  <p>DownloadData function</p>
  
  <pre><code>char* DownloadData(string url)
  {
      WSADATA wsaData;
      int iResult;
      iResult = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
      if (iResult != 0) {
  
  return "error";
      }
  
      struct addrinfo *result = NULL,
          *ptr = NULL,
          hints;
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      iResult = getaddrinfo("127.0.0.1", "49578", &amp;hints, &amp;result);
      if (iResult != 0) {
          WSACleanup();
          return "error";
      }
  
      SOCKET ConnectSocket = INVALID_SOCKET;
      ptr = result;
      ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype,
          ptr-&gt;ai_protocol);
  
      if (ConnectSocket == INVALID_SOCKET) {
          freeaddrinfo(result);
          WSACleanup();
  return "error";
      }
      iResult = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);
      if (iResult == SOCKET_ERROR) {
          closesocket(ConnectSocket);
          ConnectSocket = INVALID_SOCKET;
      }
      freeaddrinfo(result);
  
      if (ConnectSocket == INVALID_SOCKET) {
          WSACleanup();
          return "error";
      }
  
      int recvbuflen = 10000000;
      char* recvbuf = new char[recvbuflen];
      char sendbuffer[] = "GET / HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: 127.0.0.1\r\nContent-Length: 1\r\n\r\n ";
      iResult = send(ConnectSocket, sendbuffer, sizeof(sendbuffer), 0);
      if (iResult == SOCKET_ERROR) {
          closesocket(ConnectSocket);
          WSACleanup();
          return response;
      }
      iResult = shutdown(ConnectSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          closesocket(ConnectSocket);
          WSACleanup();
          return "error";
      }
      char* received = new char[512];
      int totalBytesRead = 0;
      {
          do {
              iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
              if (iResult &gt; 0) {
  
  
                  if (iResult &lt;= 0)
                      break;
  
                  received = (char*)realloc(recvbuf, iResult + totalBytesRead);
  
                  memcpy(received + totalBytesRead, recvbuf, iResult);
                  totalBytesRead += iResult;
              }
              else {
                  break;
              }
          } while (iResult &gt; 0);
      }
      closesocket(ConnectSocket);
      WSACleanup();
      char* received2 = new char[totalBytesRead];
      memcpy(received2, received, totalBytesRead);
      delete[] received;
      return received2;
  }
  </code></pre>
  
  <p>The C++ Client Http Body is messed up. Instead of just "Foo" it appears like </p>
  
  <pre><code>//headers
  
  e
  
  Foo
  8
  
  </code></pre>
  
  <p>How can I get only Foo text in the http body?</p>