java - 在 Delphi 上使用 Wininet API 的 HTTP POST

标签 java json delphi http wininet

我使用 Delphi 2010 向 Java 应用程序发送 HTTP 请求。具体来说,我正在发送一个 JSON 对象。但是,发送请求时,我不知道发生了什么,但对象不正确。

我这样发送对象:

{"entidad":"1","username":"A","password":"1234"}

我的嗅探器像这样读取对象:

%�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�

因此,我的 Java 应用程序没有读取该对象,它导致了空指针异常。

我的代码在这里:

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;  

最佳答案

JSON 默认使用 UTF-8,Delphi(2009 及更新版本)使用 UTF-16 作为默认编码。您的代码需要先将 JSON 转换为 UTF-8 字符串,然后再将其传递给 HTTPSendRequest。

我还会加一个请求头来指明编码方式,这样Java端就知道是UTF-8了。

关于java - 在 Delphi 上使用 Wininet API 的 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441120/

相关文章:

delphi - 在 FireMonkey 中创建正交投影矩阵

java - session 是否存在

java - 支付网关集成时获取 SSLException

windows - 如何在 Delphi(或通过 Windows API)的 RDP session 中唯一标识由 EasyPrint 打印机重定向的打印机?

json - nodejs JSON解析.issue

json - Bash 脚本在 Json 中动态添加新的键值对

delphi - 为整个 Delphi 应用程序设置ThreadLocale

java - this.getServletName() 在什么时间点开始工作,其原理是什么

java - 这段代码应该创建一个 JFrame 并在其上绘制......但它没有

java - Activity 类别下降