【问题标题】:use TIdHTTP in Delphi to simulate curl command with user authentication在 Delphi 中使用 TIdHTTP 模拟带有用户身份验证的 curl 命令
【发布时间】:2020-11-08 22:18:51
【问题描述】:

我尝试在Delphi中使用TIdHTTP来模拟如下curl操作:

curl -X POST -F "message={\"user_email\" : \"useremail@domain.com\" , \"user_password\" : \"UserPassword\"}" "https://esm-db.eu/esmws/generate-signed-message/1/query" > token.txt

来自服务器端的指南在:https://esm-db.eu/esmws/generate-signed-message/1/query-options.html

成功 curl 连接的详细输出如下:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 193.206.88.90...
* TCP_NODELAY set
* Connected to esm-db.eu (193.206.88.90) port 443 (#0)
* schannel: SSL/TLS connection with esm-db.eu port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 174 bytes...
* schannel: sent initial handshake data: sent 174 bytes
* schannel: SSL/TLS connection with esm-db.eu port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with esm-db.eu port 443 (step 2/3)
* schannel: encrypted data got 2954
* schannel: encrypted data buffer: offset 2954 length 4096
* schannel: sending next handshake data: sending 93 bytes...
* schannel: SSL/TLS connection with esm-db.eu port 443 (step 2/3)
* schannel: encrypted data got 258
* schannel: encrypted data buffer: offset 258 length 4096
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with esm-db.eu port 443 (step 3/3)
* schannel: stored credential handle in session cache
> POST /esmws/generate-signed-message/1/query HTTP/1.1
> Host: esm-db.eu
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Length: 217
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------7682f54661679429
>
* schannel: client wants to read 102400 bytes
* schannel: encdata_buffer resized 103424
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: encrypted data got 54
* schannel: encrypted data buffer: offset 54 length 103424
* schannel: decrypted data length: 25
* schannel: decrypted data added: 25
* schannel: decrypted data cached: offset 25 length 102400
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: decrypted data buffer: offset 25 length 102400
* schannel: schannel_recv cleanup
* schannel: decrypted data returned 25
* schannel: decrypted data buffer: offset 0 length 102400
< HTTP/1.1 100 Continue
* schannel: client wants to read 102400 bytes
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: encrypted data got 924
* schannel: encrypted data buffer: offset 924 length 103424
* schannel: decrypted data length: 895
* schannel: decrypted data added: 895
* schannel: decrypted data cached: offset 895 length 102400
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: decrypted data buffer: offset 895 length 102400
* schannel: schannel_recv cleanup
* schannel: decrypted data returned 895
* schannel: decrypted data buffer: offset 0 length 102400
< HTTP/1.1 200 OK
< Server: nginx/1.10.3
< Date: Sun, 08 Nov 2020 23:20:51 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 649
< Connection: keep-alive
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, GET, OPTIONS
<
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

但是,使用TIdHTTP 我不断收到HTTP/1.1 400 Bad Request 错误。我做错了什么?

procedure TMainForm.HTTPGetTokenFile;
var
  IdHTTP: TIdHTTP;
  Params: TIdMultipartFormDataStream;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  try
    Params := TIdMultipartFormDataStream.Create;
    Params.AddFormField('message', '{\"user_email\" : \"useremail@domain.com\" , \"user_password\" : \"UserPassword\"}');
    try
      IdHTTP := TIdHTTP.Create(nil);
      try
        LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
        LHandler.SSLOptions.Method := sslvTLSv1;
        IdHTTP.IOHandler := LHandler;
        IdHTTP.Request.Accept := 'application/json, text/plain;q=0.9, text/html;q=0.8';
        IdHTTP.Request.ContentType := 'application/json';
        Memo1.Text := IdHTTP.Post('https://esm-db.eu/esmws/generate-signed-message/1/query', Params);
      finally
        IdHTTP.Free;
      end;
    finally
      Params.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.ToString);
  end;
end;

更新:我删除了 JSON 中的 \ 字符,但我仍然收到 HTTP/1.1 400 Bad Request 错误。

procedure TMainForm.HTTPGetTokenFile;
var
  IdHTTP: TIdHTTP;
  Params: TIdMultipartFormDataStream;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  try
    Params := TIdMultipartFormDataStream.Create;
    Params.AddFormField('message', '{"user_email" : "useremail@domain.com" , "user_password" : "UserPassword"}');
    try
      IdHTTP := TIdHTTP.Create(nil);
      try
        LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
        LHandler.SSLOptions.Method := sslvTLSv1;
        IdHTTP.IOHandler := LHandler;
        IdHTTP.Request.Accept := 'application/json, text/plain;q=0.9, text/html;q=0.8';
        IdHTTP.Request.ContentType := 'application/json';
        Memo1.Text := IdHTTP.Post('https://esm-db.eu/esmws/generate-signed-message/1/query', Params);
      finally
        IdHTTP.Free;
      end;
    finally
      Params.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.ToString);
  end;
end;

TIdHTTP 的转储如下:

Stat Connected.
Sent 9/11/2020 4:36:55 ??: POST /esmws/generate-signed-message/1/query HTTP/1.0<EOL>Content-Type: multipart/form-data; boundary=--------110920163653921<EOL>Content-Length: 257<EOL>Host: esm-db.eu<EOL>Accept: application/json, text/plain;q=0.9, text/html;q=0.8<EOL>Accept-Encoding: identity<EOL>User-Agent: Mozilla/3.0 (compatible; Indy Library)<EOL><EOL>
Sent 9/11/2020 4:36:55 ??: ----------110920163653921<EOL>Content-Disposition: form-data; name="message"<EOL>Content-Type: text/plain<EOL>Content-Transfer-Encoding: quoted-printable<EOL><EOL>{"user_email" : "s.antoniou@seismosoft.com" , "user_password" : "passw=<EOL>ord"}<EOL>----------110920163653921--<EOL>
Recv 9/11/2020 4:36:55 ??: HTTP/1.1 400 Bad Request<EOL>Server: nginx/1.10.3<EOL>Date: Mon, 09 Nov 2020 14:36:56 GMT<EOL>Content-Type: text/html; charset=UTF-8<EOL>Content-Length: 121<EOL>Connection: close<EOL>Access-Control-Allow-Origin: *<EOL>Access-Control-Allow-Methods: POST, GET, OPTIONS<EOL><EOL>{"http_code": 400, "http_label": "Bad Request", "exit_message": "ERROR: improper specification / unrecognized parameter"}
Stat Disconnected.
Stat Disconnected.
Stat Disconnected.

AddFormField() 函数的争论似乎有 70 个字符的限制,最后 3 个字符似乎被截断了。我可以增加这个限制还是应该减少传递的值的大小(例如,通过删除不必要的空格)?

【问题讨论】:

  • 我怀疑你需要使用\字符来转义Delphi版本中的双引号
  • 您提供了 curl 请求的部分转储(仅标头,没有正文数据),但您没有提供 TIdHTTP 请求的任何转储以进行比较。请提供两个请求的完整转储,否则我们无法准确查看 TIdHTTP 请求与 curl 请求的不同之处,以了解服务器可能认为什么是坏的。
  • 对于 curl 您的意思是通过 -verbose Debug 或其他方式输出内容?关于 TIdHTTP,我苦苦挣扎了好几个小时才弄清楚该怎么做。使用 IdLogDebug 我找不到保存文件数据或将它们导出到 TMemo 的方法,使用 TIdInterceptSimLog 我可以指定一个文件,但我不断收到 105 I/O 错误并且创建的文件没有内容。跨度>
  • @SteliosAntoniou 对于 curl,试试 --trace option。对于 TIdHTTP,请尝试 TIdLogFile
  • @Remy 非常感谢您的及时回复。我会试一试,然后再找你。干杯

标签: delphi curl indy10


【解决方案1】:

正如 Dave 在评论中提到的,您需要删除 JSON 数据中的 \ 字符。 Delphi 不会像命令行处理器那样转义字符。 " 不是 Delphi 中的保留字符,因此无需在 Delphi 字符串文字中转义 " 字符。

另外,附带说明一下,IdHTTP.Request.ContentType 的分配在这种情况下是多余的,应该删除。 Post()'ing a TIdMultiPartFormDataStream 将用自己的值覆盖ContentType,忽略您分配的任何内容。

试试这个:

procedure TMainForm.HTTPGetTokenFile;
var
  IdHTTP: TIdHTTP;
  Params: TIdMultipartFormDataStream;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  try
    Params := TIdMultipartFormDataStream.Create;
    try
      Params.AddFormField('message', '{"user_email" : "useremail@domain.com" , "user_password" : "UserPassword"}');

      IdHTTP := TIdHTTP.Create(nil);
      try
        LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
        LHandler.SSLOptions.Method := sslvTLSv1;
        IdHTTP.IOHandler := LHandler;
        IdHTTP.Request.Accept := 'application/json, text/plain;q=0.9, text/html;q=0.8';
        Memo1.Text := IdHTTP.Post('https://esm-db.eu/esmws/generate-signed-message/1/query', Params);
      finally
        IdHTTP.Free;
      end;
    finally
      Params.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.ToString);
  end;
end;

更新: 原来TIdMultiPartFormDataStream 正在以Quoted-Printable 格式发送您的 JSON,其中每 70 个字符会在您的 JSON 中间插入一个“软”换行符。显然,您的服务器不正确支持该编码。默认情况下,curl 不会使用Quoted-Printable 提交网络表单,除非您明确告诉它这样做。

您可以通过以下任一方式禁用TIdMultipartFormDataStream 中的Quoted-Printable 编码:

  1. TIdFormDataField.ContentTransfer 属性设置为'7bit''8bit''binary'''(实际上与'7bit' 相同,但不会通过Content-Transfer-Encoding 标头显式通知服务器) ,例如:
procedure TMainForm.HTTPGetTokenFile;
var
  IdHTTP: TIdHTTP;
  Params: TIdMultipartFormDataStream;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  try
    Params := TIdMultipartFormDataStream.Create;
    try
      with Params.AddFormField('message', '{"user_email" : "useremail@domain.com" , "user_password" : "UserPassword"}') do
      begin
        Charset := 'utf-8';
        ContentTransfer := '8bit';
      end;

      IdHTTP := TIdHTTP.Create(nil);
      try
        LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
        LHandler.SSLOptions.Method := sslvTLSv1;
        IdHTTP.IOHandler := LHandler;
        IdHTTP.Request.Accept := 'application/json, text/plain;q=0.9, text/html;q=0.8';
        Memo1.Text := IdHTTP.Post('https://esm-db.eu/esmws/generate-signed-message/1/query', Params);
      finally
        IdHTTP.Free;
      end;
    finally
      Params.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.ToString);
  end;
end;
  1. 使用采用TStream 而不是StringAddFormField() 重载。您可以将您的 JSON 放入 TStringStream,例如:
procedure TMainForm.HTTPGetTokenFile;
var
  IdHTTP: TIdHTTP;
  Params: TIdMultipartFormDataStream;
  LHandler: TIdSSLIOHandlerSocketOpenSSL;
  LJSON: TStringStream;
begin
  try
    LJSON := TStringStream.Create('{"user_email" : "useremail@domain.com" , "user_password" : "UserPassword"}', TEncoding.UTF8);
    try
      Params := TIdMultipartFormDataStream.Create;
      try
        with Params.AddFormField('message', 'text/plain', 'utf-8', LJSON) do
          ContentTransfer := '8bit';
    
        IdHTTP := TIdHTTP.Create(nil);
        try
          LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
          LHandler.SSLOptions.Method := sslvTLSv1;
          IdHTTP.IOHandler := LHandler;
          IdHTTP.Request.Accept := 'application/json, text/plain;q=0.9, text/html;q=0.8';
          Memo1.Text := IdHTTP.Post('https://esm-db.eu/esmws/generate-signed-message/1/query', Params);
        finally
          IdHTTP.Free;
        end;
      finally
        Params.Free;
      end;
    finally
      LJSON.Free;
    end;
  except
    on E: Exception do
      ShowMessage('Error: ' + E.ToString);
  end;
end;

【讨论】:

  • 感谢您的回答。不幸的是,即使没有 \ 字符,我仍然会收到“HTTP/1.1 400 Bad Request”错误。
  • @SteliosAntoniou 那么您的 TIdHTTP 设置还有其他问题,我们看不到。您只需要捕获 curl 和 TIdHTTP 生成的实际请求数据并比较它们的差异,然后相应地调整您的 Delphi 代码。
  • 不确定我是否理解,抱歉。如何捕获生成的数据?关于我的 TIdHTTP 设置,你的意思是在我的 Indy 安装中,对吧?
  • @SteliosAntoniou 不,我的意思是属性设置。显然 curl 和 TIdHTTP 之间有些不同。要捕获原始请求,由于您使用的是 HTTPS,因此您可以启用 curl 的 verbosetrace 输出,并将 Indy 的 TIdLog... 组件之一附加到 TIdHTTP.Intercept 属性。
  • '@RemyLeeau' 谢谢。我会试一试(我需要花一些时间学习),如果我仍然无法让它工作,我会回来找你们的。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 2011-07-06
  • 2015-11-13
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
相关资源
最近更新 更多