title: HTTP POST with Wininet API on Delphi
body: <p>I'm using Delphi 2010 for sending an HTTP request to a Java application. Specifically, I'm sending a JSON object. But, when sending the request, I don't know what's happening, but the object is not correct. </p>
  
  <p>I send the object like this:</p>
  
  <pre><code>{"entidad":"1","username":"A","password":"1234"}
  </code></pre>
  
  <p>My sniffer reads the object like this:</p>
  
  <pre><code>%�7�B�%�2�2�e�n�t�i�d�a�d�%�2�2�%�3�A�%�2�2�8�3�0�0�2�3�0�0�0�%�2�2�%�2�C�
  </code></pre>
  
  <p>Therefore, my Java application doesn't read the object and it's causing a null pointer exception.</p>
  
  <p>My code is here:</p>
  
  <pre><code>function TFormMain.JSONPostRequest(Server,Url,jo : String; blnSSL: Boolean): String;
  var
    aBuffer     : Array[0..4096] of Char;
    Header      : TStringStream;
    BufStream   : TMemoryStream;
    BytesRead   : Cardinal;
    pSession    : HINTERNET;
    pConnection : HINTERNET;
    pRequest    : HINTERNET;
    port        : Integer;
    flags       : DWord;
  begin
    Result := '';
    pSession := InternetOpen(nil, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
    if Assigned(pSession) then
    try
      if blnSSL then
        Port := INTERNET_DEFAULT_HTTPS_PORT
      else
        Port := 9000;
      pConnection := InternetConnect(pSession, PChar(Server), port, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
      if Assigned(pConnection) then
      try
        if blnSSL then
          flags := INTERNET_FLAG_SECURE or INTERNET_FLAG_KEEP_CONNECTION
        else
          flags := INTERNET_SERVICE_HTTP;
        pRequest := HTTPOpenRequest(pConnection, 'POST', PChar(Url), nil, nil, nil, flags, 0);
        if Assigned(pRequest) then
        try
          Header := TStringStream.Create('');
          try
            with Header do
            begin
              WriteString('Host: ' + Server + ':' + IntToStr(Port) + sLineBreak);
            end;
            HttpAddRequestHeaders(pRequest, PChar(Header.DataString), Length(Header.DataString), HTTP_ADDREQ_FLAG_ADD);
            if HTTPSendRequest(pRequest, nil, 0, Pointer(jo), Length(jo)) then
            begin
              BufStream := TMemoryStream.Create;
              try
                while InternetReadFile(pRequest, @aBuffer, SizeOf(aBuffer), BytesRead) do
                begin
                  if (BytesRead = 0) then Break;
                  BufStream.Write(aBuffer, BytesRead);
                end;
                aBuffer[0] := #0;
                BufStream.Write(aBuffer, 1);
                Result := WideCharToString(PChar(BufStream.Memory));
              finally
                BufStream.Free;
              end;
            end
            else
              raise Exception.Create('HttpOpenRequest failed. ' + SysErrorMessage(GetLastError));
          finally
            Header.Free;
          end;
        finally
          InternetCloseHandle(pRequest);
        end;
      finally
        InternetCloseHandle(pConnection);
      end;
    finally
      InternetCloseHandle(pSession);
    end;
  end;  
  </code></pre>